Thymeleaf Attributes

Learn how to use Thymeleaf expressions inside Thymeleaf attributes.

The Thymeleaf expressions we just saw can be used inside Thymeleaf attributes like the th:text attribute. This lesson will go over the most important attributes.

Element text content

The th:text attribute will place the result of the expression inside the tag it is declared on.

For example, assuming the username variable contains “Jane”:

<div th:text="${username}">Bob</div>

will render as:

<div>Jane</div>

Element id attribute

Similarly, the th:id attribute will add an id attribute.

<div th:id="|container-${userId}|"></div>

will render (userId will be 1 here) as:

<div id="container-1"></div>

Conditional inclusion

We can choose to render a tag subject to some condition using the th:if attribute. For example:

<div th:if="${user.followerCount >
...