Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape double quote " in thymeleaf?

Tags:

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?

like image 689
hilias Avatar asked Dec 11 '17 17:12

hilias


People also ask

How do you escape a double quote?

“Double quotes 'escape' double quotes“ When using double quotes "" to create a string literal, the double quote character needs to be escaped using a backslash: \" .

How do you escape Thymeleaf?

key}" + 'what\'s up' " context.

How do you escape quotation marks in a string?

2. How to Escape Quotes in a String. To add quoted strings inside of strings, you need to escape the quotation marks. This happens by placing a backslash ( \ ) before the escaped character.


1 Answers

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.

like image 84
Metroids Avatar answered Sep 20 '22 12:09

Metroids