Kadzhaev Marat
Kadzhaev Marat

Reputation: 1317

HTML tags inside Thymeleaf

I need write text like: <td th:text="${ticket.eventName} + '<br />' + ${ticket.ticketType}">Event Name</td> But Thymeleaf return error because of <br />. How I can solve this problem?

UPD: I try make like: <td th:text="${ticket.eventName} + #{nextline} + ${ticket.ticketType}">Event Name</td> and this works. nextline value = \n, but #{nextline} works only one time. If I paste it repeatedly it doesn't work, why?

UPD2: I solve this problem. Instead '<br />' need paste '&lt;br /&gt;' and th:text change to th:utext.

Upvotes: 0

Views: 4456

Answers (3)

Grigory Kislin
Grigory Kislin

Reputation: 18030

Tested for Thymeleaf 3.0:

<td th:utext="|${ticket.eventName} &lt;br/&gt; ${ticket.ticketType}|">Event Name</td>

Take note of th:utext tag

Other option:

<td>[[${ticket.eventName}]] <br/> [[${ticket.ticketType}]]</td>

See Inlined output expressions

Upvotes: 3

Sergio Garcia Alonso
Sergio Garcia Alonso

Reputation: 951

You can use th:inline:

<td th:inline="text">
    [[${ticket.eventName}]]
    <br/>
    [[${ticket.ticketType}]]
</td>

More info: http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#inlining

Upvotes: 3

Lukehey
Lukehey

Reputation: 1354

If you want to skip escaping the characters you can use th:block, which produces cleaner results.

th:block is a mere attribute container that allows template developers to specify whichever attributes they want. Thymeleaf will execute these attributes and then simply make the block dissapear without a trace. (http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#synthetic-thblock-tag)

So in your example:

<td>
    <th:block th:text="${ticket.eventName}"/>
    <br/>
    <th:block th:text="${ticket.ticketType}"/>
</td>

Upvotes: 2

Related Questions