Thymeleaf: How to exclude an external tag when using th: each?

The Thymeleaf 2.1.4 white paper demonstrates the use of for each , as shown 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> at each iteration, which is ideal in this situation. However, in my case, I do not need an external tag (here <tr> ).


My use case is to generate the <bookmark> recursive way, other tags do not include, and the <bookmark> should contain the 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> 

switching side:

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

Many thanks.

+7
java template-engine thymeleaf
source share
3 answers

Even if this can be done with th:remove="tag" , I 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> 
+20
source share

You can use the DIV tag or any other HTML tag for the loop. This will not create a TR tag. But for the table to render correctly, you need to have TD tags inside the 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> 
0
source share

I figured out how to solve the problem, easy, just add th:remove="tag" to the external tag.

0
source share

All Articles