17

I'm new to thymeleaf and am trying to make a simple table using an array and an each loop.

My code looks like this:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Smoke Tests</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<table border="1" style="width:300px">
    <tr>
        <td>Test Name</td>
    </tr>
    <tr th:each="smokeTest : ${smokeTests}">
        <td>
            th:text="${smokeTest.name}">A Smoke Test'
        </td>
    </tr>
</table>
</body>
</html>

Basically my problem is that I can't run the loop as <td>s within <tr>s. Is there any way that this code could work?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
user3073234
  • 771
  • 5
  • 11
  • 24

3 Answers3

14

You must put th:text as an attribute of a tag, so

<tr th:each="smokeTest : ${smokeTests}">
   <td th:text="${smokeTest.name}">A Smoke Test'</td>
</tr>

should run.

niels
  • 7,321
  • 2
  • 38
  • 58
10

Simple solution which comes to mind first:

<th:block th:each="smokeTest : ${smokeTests}">
    <tr>
        <td th:text="${smokeTest.name}">A Smoke Test'</td>
    </tr>
</th:block>

Details: http://www.thymeleaf.org/whatsnew21.html#bloc

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69
1

Although, it's late answer. It's work more specifically, like

<tr th:each="smokeTest : ${smokeTests}">
   <td><p th:text="${smokeTest.name}"></p></td>
</tr>
Rashed
  • 124
  • 12