Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a string that contains HTML in Thymeleaf template

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

like image 471
superkytoz Avatar asked Oct 14 '15 22:10

superkytoz


People also ask

How do you display strings in Thymeleaf?

In this case, you should use th:text . For example, Username: <p th:text=${account. username}>Username will be rendered here</p> .

How do I enable Thymeleaf in HTML?

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.

How do I print the value of a variable in Thymeleaf?

You can use the "th:text=#{variable}" to print a variable in the Thymeleaf.

What is #{} in Thymeleaf?

#{} is used for message (i18n) expressions. Used to retrieve locale-specific messages from external sources.


1 Answers

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.

like image 169
Balwinder Singh Avatar answered Oct 19 '22 10:10

Balwinder Singh