28

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

2 Answers 2

49

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>
Sign up to request clarification or add additional context in comments.

Comments

3

This pretty straight forward. You can do this:

<!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:each="smokeTest : ${smokeTests}" th:text="${smokeTest.name}"><p/>
</body>
</html>

You can also use some other html tags other than paragraph tag. Like this:

<h2 th:each="smokeTest : ${smokeTests}" th:text="${smokeTest.name}"><h2/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.