Underscores in Thymeleaf Text Literals

Viewed 467

Question: How to escape multiple consecutive underscores in text literals?

I am using the standard Thymeleaf dialect for HTML (I am not using Spring or SpEL here).

In Thymeleaf, I can create an underscore as a text literal as follows:

<div th:text="'_'"></div>

This renders as:

<div>_</div>

I can create literals with 2 and 3 underscores in the same way:

<div th:text="'__'"></div>
<div th:text="'___'"></div>

But for 4 underscores, I get an error:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: ""

I assume (maybe incorrectly) this is because two pairs of underscores (__ followed by __) are the markers used by Thymeleaf for the expression preprocessor. And when these are removed, I am left with an empty expression - hence the error.

I can escape the underscores using the backslash (\) escape character. The following all give the required results:

<div th:text="'\_\___'"></div>
<div th:text="'\_\_\_\__'"></div>
<div th:text="'\_\_\_\___'"></div>
<div th:text="'_\_\_\_\___'"></div>
<div th:text="'\_\_\_\_\_\___'"></div>

But I can't just escape every underscore.

This displays a stray backslash:

<div th:text="'\_\_\_\_\_'"></div>

The result is:

<div>____\_</div>

So:

  1. What are the rules for escaping underscores in text literals?

  2. Is it really the preprocessor which is causing this behavior (inside text literals) - or is it something else?

1 Answers

Yeah, this is definitely part of the preprocessor.

It looks to me like the preprocessor only replaces an exact match of \_\_ with __. In any case where you have an odd number of \_'s, you will get the output \_ -- because it's not treating \_ as a real escape and instead only looking for \_\_.

Related