10

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

superkytoz
  • 1,267
  • 4
  • 23
  • 43

1 Answers1

26

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.

Balwinder Singh
  • 2,272
  • 5
  • 23
  • 34