Skip to content
Advertisement

Thymeleaf – output variable without a tag

I use Thymeleaf as a templating engine and I usually output variable value like this:

in Java I set:

ctx.setVariable("tester", "hello");

and in html template I output:

<span th:text="${tester}"></span>

This works great, but I would like to output a variable without the need of a tag. Something following would be great:

${tester}

Unfortunately it does not work. My goal is to avoid unnecessary tag to output the variable value. Is this possible to do with Thymeleaf?

Advertisement

Answer

My goal is to avoid unnecessary tag to output the variable value. Is this possible to do with Thymeleaf?

Yes this is possible. You can use the Thymeleaf synthetic th:block tag (see here).

Example template excerpt:

<body>
    <th:block th:text="${tester}"></th:block>    
</body>

This renders the following HTML:

<body>
    hello    
</body>

Only the variable is displayed.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement