Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape single quote (') in Thymeleaf

Tags:

<h1 th:text="${'What\'s up?'}"></h1>

I want this to output

<h1>What's up?</h1>

But I get an TemplateInputException. I have tried with HTML entity but it fails the same.

like image 328
b2238488 Avatar asked Mar 30 '17 15:03

b2238488


People also ask

How do you escape a single quote?

Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings.

How do you escape a single quote in curl command?

The backslash ( \ ) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

How do you handle a single quote in a string?

A single quote is not used where there is already a quoted string. So you can overcome this issue by using a backslash following the single quote. Here the backslash and a quote are used in the “don't” word. The whole string is accompanied by the '$' sign at the start of the declaration of the variable.


2 Answers

Double single quote. Like this:

<h1 th:text="${'What''s up?'}" /> 
like image 118
Metroids Avatar answered Oct 31 '22 02:10

Metroids


To escape a single quote you just escape it with a \'

<p th:text="'What\'s up?'"></p> <p th:text="${myVar} + 'What\'s up?'"></p> 
like image 22
RenRen Avatar answered Oct 31 '22 03:10

RenRen