Thymeleaf - output variable without a tag

Viewed 1010

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?

4 Answers

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.

Thymeleaf triggers on the "th:" tag and as far as I know thats the only way. The behaviour you describe works with JSF.

Best regards

Ben

I also managed to figure out some workaround:

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

th:remove removes span tag, but preserves content.

Use Thymeleaf expression inlining (docs) using either [[...]] or [(...)]. With expression inlining, you do not need to use synthetic tags.

Example:

<body>
The value of tester is [[${tester}]].
</body>
Related