How can I display a string that contains HTML tags in Thymeleaf?
So this piece of code:
<div th:each="content : ${cmsContent}">
<div class="panel-body" sec:authorize="hasRole('ROLE_ADMIN')">
<div th:switch="${content.reference}">
<div th:case="'home.admin'">
<p th:text="${content.text}"></p>
</div>
</div>
</div>
//More code....
And at this line of piece of code ${content.text}
it literally generates this on the browser:
<p>test</p>
But I want to show this instead on the browser:
test
In this case, you should use th:text . For example, Username: <p th:text=${account. username}>Username will be rendered here</p> .
Thymeleaf Implementation We can implement Thymeleaf template engine by adding spring-boot-starter-thymeleaf dependency in our application's pom. xml file. Spring Boot configures template engine to read template file from /resource/templates.
You can use the "th:text=#{variable}" to print a variable in the Thymeleaf.
#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.
You can use th:utext
(unescaped text) for such scenarios.
Simply change
<p th:text="${content.text}"></p>
to
<p th:utext="${content.text}"></p>
I will suggest to also have a look into documentation here to know all about using Thymeleaf.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With