Bluescreen
Bluescreen

Reputation: 154

Thymeleaf - Insert HTML into <p>

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 &lt;b&gt;test&lt;/b&gt;"

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

Answers (1)

Metroids
Metroids

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 &lt;br&gt;test&lt;/br&gt;", 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

Related Questions