2015-06-18 27 views
7

的Thymeleaf 2.1.4官方文档演示for each用法如下:Thymeleaf:如何在使用th时排除外部标签:每个?

<tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'"> 
    <td th:text="${prod.name}">Onions</td> 
    <td th:text="${prod.price}">2.41</td> 
    ... 
</tr> 

它产生在每个迭代中,这是在这种情况下,完美契合一个<tr>。但在我的情况下,我不需要外部标签(在这里,<tr>)。


我用例是一个递归的方式产生<bookmark>标签,没有任何其他标记包括,和<bookmark>标签必须包含名称和href属性。

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> 
<body> 

<div th:fragment="locationBookmark(location)"> 
    <bookmark th:each="map : ${location.subMaps}"> 
     <bookmark th:name="${map.name}" 
        th:href="'#'+${map.id}" th:include=":: locationBookmark(${map})"> 
     </bookmark> 
    </bookmark> 
</div> 
</body> 
</html> 

的包边:

<bookmark th:include="bookmark : locationBookmark(${rootLocation})"/> 

非常感谢。

+0

你能提供标签的样品要不是附近的外标签? –

+0

@PavelUvarov我的用例添加,请参阅我的问题。 – July

+0

让我猜测:你想制作n级别的儿童标签吗?我认为是因为:书签不是html标签,因为不建议不要关闭带有内容的标签。如果我是对的 - 你应该以递归的方式创建和使用一些子模板。我不想让ThymeLeaf说你应该怎么做,但我确定知道这是可能的 - 请仔细阅读他们的文档。 –

回答

20

即使能使用th:remove="tag"做我会建议你使用th:block

<th:block th:each="map : ${location.subMaps}"> 
    <bookmark th:name="${map.name}" 
     th:href="'#'+${map.id}" th:include=":: locationBookmark(${map})"> 
    </bookmark> 
</th:block> 
+0

很高兴知道,非常感谢 – July

+0

@Xipo我更正了 ..希望对于所有Thymeleaf版本都是正确的,但是因为XML是潜在的... –

0

您可以使用DIV标记或任何其他HTML标记进行循环。这不会生成TR标记。但是要正确呈现表格,您需要在TR标记内部有TD标记。

<div th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'"> 
    <td th:text="${prod.name}">Onions</td> 
    <td th:text="${prod.price}">2.41</td> 
</div> 
+0

我想要的是完全删除标签,而不是更改其名称。 – July

0

我想通了,如何解决这个问题,这很容易,只需添加th:remove="tag"到外标签就行了。

相关问题