'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?
Solution 1:[1]
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
Solution 2:[2]
I also managed to figure out some workaround:
<span th:text="${tester}" th:remove="tag"></span>
th:remove removes span tag, but preserves content.
Solution 3:[3]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | bemar |
Solution 2 | michal.jakubeczy |
Solution 3 |