Search code examples
thymeleaf

How to escape double quote " in thymeleaf?


I want to put double quotes in a String in Thymeleaf, I have something of the form:

<td th:text='${"Value of \"" + item + "\" is \"" + value + "\"."}'></td>

The result i want is:

<td>Value of "apple" is "1.5".</td>

But I get the following exception:

EL1065E: unexpected escape character.

How can I do this?


Solution

  • I'm not sure it's possible. Something like this works:

    th:text='|Value of "${item}" is "${value}".|'
    

    I would personally write it like this:

    th:text="${'Value of &quot;' + item + '&quot; is &quot;' + value + '&quot;.'}"
    

    I think the reason there is no way to escape double quotes, is that thymeleaf is first parsing as xml/html (which has no escape other than &quot;) and then parsing as thymeleaf second which doesn't really have a chance to get at those strings.