Chris
Chris

Reputation: 943

Thymeleaf string substitution and escaping

I have a string which contains raw data, which I want escaped. The string also contains markers which I want to replace with span tags.

For example my string is

"blah {0}something to span{1} < random chars <"

I would like the above to be rendered within a div, and replace {0} with and {1} with

I have tried a number of things, including doing the substitution in my controller, and trying to use the th:utext attribute, however I then get SAX exceptions.

Any ideas?

Upvotes: 0

Views: 3332

Answers (2)

Tom
Tom

Reputation: 429

It looks using message parameters is the right approach to output formatted strings. See http://www.thymeleaf.org/doc/usingthymeleaf.html#messages

I suspect you need to pass character entity reference in order to avoid SAX exceptions

<span th:utext = "#{string.pattern(${'&lt;span&gt;john&lt;/span&gt;'}, ${'&lt;span&gt;doe&lt;/span&gt;'})}"/>

Alternatively place the markup in your .properties file:

string.pattern=my name is <span>{0}</span> <span>{1}</span>

Upvotes: 2

fliim
fliim

Reputation: 2189

You can do this using i18n ?

something like:

resource.properties:

string.pattern=my name is {0} {1}

thymeleaf view:

<label th:text="#{__${#string.pattern('john', 'doe')}__}"></label>

The result should be:

my name is john doe

Im not sure this is a good way. But I hope it could help you

Upvotes: 1

Related Questions