The Thymeleaf 2.1.4 official doc demonstrates the for each
usage as below:
<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>
It generates one <tr>
in each iteration, which is perfect fit in this situation. However in my case I don't need the outer tag (here, <tr>
).
My use case is to generating <bookmark>
tag in a recursive way, no other tags include, and a <bookmark>
tag must contain a name and href attribute.
<!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>
the including side:
<bookmark th:include="bookmark : locationBookmark(${rootLocation})"/>
Many thanks.
Even if it can be done using th:remove="tag"
I would suggest you use 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>
我想出了如何解决这个问题,很简单,只需将th:remove="tag"
到外部标签即可。
You can use a DIV
tag or any other HTML tag to loop with. This will not generate a TR
tag. But for the table to render correctly you need to have the TD
tags inside of TR
tags.
<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>
This isn't exactly what the OP asked for but it could be useful to some:
A tag can also be removed conditionally :
<div class="myConditionalOuterDiv" th:remove="${condition}? tag"}>
<span> I'm always there </span>
</div>
This removes the parent element but leaves the children elements in place.
Source here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.