Reputation: 1508
I am trying to use Thymelead as a template engine for some text templates that I have.
My template looks like this
free text
[[${myVar}]]
more text
My Java code is
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode(TEXT);
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
Context context = new Context(ENGLISH, null);
context.setVariable("myVar", "My text with special characters such as ' < > &");
System.out.println(templateEngine.process("templateFileName", ctx));
I am expecting the above to print
free text
My text with special characters such as ' < > &
more text
but instead it prints
free text
My text with special characters such as ' < > &
more text
Since I am working on plain text and not HTML or XML shouldn't it print the special characters unescaped ?
I have set mode to TEXT and encoding to UTF-8.
Please enlighten me.
Thanks
Nick
Upvotes: 3
Views: 5821
Reputation: 3717
Change your template from:
free text
[[${myVar}]]
more text
...to:
free text
[(${myVar})]
more text
As stated in Thymealeaf's documentation:
Expressions between
[[...]]
or[(...)]
are considered inlined expressions in Thymeleaf, and inside them we can use any kind of expression that would also be valid in ath:text
orth:utext
attribute.Note that, while
[[...]]
corresponds toth:text
(i.e. result will be HTML-escaped),[(...)]
corresponds toth:utext
and will not perform any HTML-escaping
Upvotes: 1
Reputation: 20487
You should use [(${myVar})]
to print the text unescaped.
(I do agree that it's weird that [[${...}]]
expressions escape HTML special characters in TEXT template mode.)
Upvotes: 3