Reputation: 154
I have the following Java variables:
context.setVariable("content", "test <b>test</b>");
context.setVariable("condition", condition);
and I want to place this into the following template:
<p th:text="${condition}?${content}:''"></p>
The expected result is: "test test" if the condition is true. However, the result is
"test <b>test</b>"
if I open my page and the condition is true. Changing th:text=...
to th:utext=...
results in
"test <b>test</b>"
displayed on my page, which is still not what I want.
Is there a way to do this?
Upvotes: 1
Views: 852
Reputation: 20487
If you want the actual HTML, then you need to pass actual (not escaped) HTML and use th:utext
. (It doesn't make sense to pass in escaped HTML and think Thymeleaf will unescape it for you.)
// Controller
context.setVariable("content", "test <br>test</br>");
<!-- HTML -->
<p th:utext="${condition}?${content}:''"></p>
If you really do want to pass in "test <br>test</br>"
, then you're going to have to use string search/replace to change those characters back into <
and >
(probably using the #strings
utility object).
Upvotes: 2