【发布时间】:2015-09-04 01:10:57
【问题描述】:
Thymeleaf 2.1.4 官方文档演示了for each 的用法如下:
<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>标签必须包含name和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})"/>
非常感谢。
【问题讨论】:
-
你能提供你不想被外部标签关闭的标签样本吗?
-
@PavelUvarov 我的用例已添加,请参阅我的问题。
-
让我猜猜:你想制作 n 级子标签?我这么认为是因为:书签不是 html 标签,并且因为不建议不关闭带有内容的标签。如果我是对的 - 你应该以递归方式创建和使用一些子模板。我不太擅长在 ThymeLeaf 中说你应该怎么做,但我当然知道这是可能的 - 请仔细查看他们的文档。
标签: java template-engine thymeleaf