Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For each operator in Thymeleaf

I can not find syntax for building simple for-each-loop in Thymeleaf template. I'm not satisfied with just th:each="" attribute, because it copies tag in which it's located.

What I'm looking for is something like:

<th:foreach th:each="..."> ...block to be repeated... </th> 

what is analogue of <c:forEach items="..." var="..."> or <t:loop source="..." value="..."> in Tapestry. Is anything similar for that?

like image 452
Andremoniy Avatar asked Apr 20 '16 13:04

Andremoniy


People also ask

How do you use each in Thymeleaf?

We will use th:each to iterate through the list of customers and list of addresses for each customer. CustomerController class was defined to handle all GET requests to /customers URI and return a rendered page customers. html as an output (which is our Thymeleaf template located in /resources/templates ).

How do you give the condition in Thymeleaf?

In some situations, you want a certain snippet of the Thymeleaf Template to appear in the result if a certain condition is evaluated as true. To do this you can use the attribute th:if. Note: In Thymeleaf, A variable or an expression is evaluated as false if its value is null, false, 0, "false", "off", "no".

How do you break the loop in Thymeleaf?

The best solution would be the put this logic in the controller and put the first product of type 'T' in a separate attribute. If that's not possible, another solution would be to write a Thymeleaf extension (or if using Spring a bean) that does this.


2 Answers

Use th:block as stated in the Thymeleaf guide

th:block is a mere attribute container that allows template developers to specify whichever attributes they want. Thymeleaf will execute these attributes and then simply make the block disappear without a trace.

So it could be useful, for example, when creating iterated tables that require more than one <tr> for each element:

<table>    <th:block th:each="user : ${users}">       <tr>          <td th:text="${user.login}">...</td>          <td th:text="${user.name}">...</td>       </tr>       <tr>          <td colspan="2" th:text="${user.address}">...</td>       </tr>    </th:block> </table> 
like image 79
ekem chitsiga Avatar answered Nov 07 '22 10:11

ekem chitsiga


The th:block solution is definitely the best one, but alternatively you can also try using th:remove="tag" in order to remove the containing tag:

<table>    <tbody th:each="user : ${users}" th:remove="tag">       <tr>          <td th:text="${user.login}">...</td>          <td th:text="${user.name}">...</td>       </tr>       <tr>          <td colspan="2" th:text="${user.address}">...</td>       </tr>    </tbody> </table> 

The benefit of this approach is that you can also pass a Thymeleaf expression to th:remove in order to only remove the tag conditionally, e.g. if you want only some users to be included in a <tbody>, besides having other interesting uses.

Here is the documentation for th:remove.

like image 29
Raibaz Avatar answered Nov 07 '22 08:11

Raibaz