Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through array in ThymeLeaf

I am new to ThymeLeaf and I was wondering if there was a way to loop around a <p> html tag as well as iterate through an array within that <p> tag. I want the elements within smokeTest to end up in different paragraphs.

<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head>     <title>Getting Started: Serving Web Content</title>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> </head> <body> <p th:text="${smokeTests[0].name}"/> </body> </html> 

Thanks for your help

like image 217
user3073234 Avatar asked Jun 27 '14 20:06

user3073234


People also ask

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.

How do I index a list in Thymeleaf?

Thymeleaf maintains the iteration status of the th:each tag in a special variable. Please, see the relevant documentation. Among the different information provided in that variable, you can find an index property, which corresponds to the actual iteration index. You are welcome @PublicDisplayName.

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 ).


1 Answers

Did you try the following code? I didn't test it, cause it's often used :

<body>     <p th:each="smokeTest : ${smokeTests}"        th:text="${smokeTest.name}">A Smoke Test</p> </body> 
like image 135
Jad B. Avatar answered Sep 20 '22 18:09

Jad B.