thymeleaf

Thymeleaf and macro substitution


folks...

Can I do something like this using Thymeleaf?

<tr th:each="row : ${list}">
    <td th:each="field : ${arrayFieldName}" scope="row">
        <span th:text="|${row.${field[0]}}|"></span>
    </td>
</tr>

I can use:

<span th:text="|${row.nameOfMyField1}|"></span>
<span th:text="|${row.nameOfMyField2}|"></span>

that works fine... but in this case I need to perform the th:each for every field name that I have in ${arrayFieldName}.

How can I do this?


Solution

  • Yes, this is called preprocessing in Thymeleaf. It should look like this:

    <tr th:each="row : ${list}">
        <td th:each="field : ${arrayFieldName}" scope="row">
            <span th:text="|${row.__${field}__}|"></span>
        </td>
    </tr>