Search code examples
thymeleafspring-thymeleaf

Thymeleaf: interpolate variables without tag


Using thymeleaf in spring boot project. Got a template with long texts and was wondering if it is possible to interpolate variables without using a tag. For example, instead of:

<div th:if="${someCondition}" th:remove="tag">
Foo bar long text <span th:text="${someVariable}" th:remove="tag"/>,lorem ipsum <span th:text="${anotherVariable}" th:remove="tag"/>
<span th:text="${thirdVariable}" th:remove="tag" />.
</div>

Something similar to this (e.g.: handlebars):

<div th:if="${someCondition}" th:remove="tag">
Foo bar long text {{someVariable}}, lorem ipsum {{anotherVariable}} {{thirdVariable}}.
</div>

The latter I found a lot easier to read and work with.


Solution

  • You can do this with expression inlining.

    <div th:if="${someCondition}" th:remove="tag">
      Foo bar long text [[${someVariable}]], lorem ipsum [[${anotherVariable}]] [[${thirdVariable}]].
    </div>