Loop through array in ThymeLeaf

user3073234 picture user3073234 · Jun 27, 2014 · Viewed 40.6k times · Source

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

Answer

Jad B. picture Jad B. · Jun 28, 2014

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>