0

I use Thymeleaf for email templates. Since HTML email templates are a bit of a pain in the *ss, I want to declare some values in variables and then use them whenever needed inside the template. For example, you have to put into each style attribute for tags holding text the complete font-family.

So my approach is something like this:

<div th:with="fontFamilySans='font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI Adjusted\', \'Segoe UI\', \'Liberation Sans\', Roboto, \'Helvetica Neue\', Arial, sans-serif;'">

  <p th:style="|margin: 0; padding: 0; ${fontFamilySans}|">Aloha from Hawaii</p>

</div>

Unfortunaltely, doing it this way the single quotes get escaped:

<p style="margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, &#39;Segoe UI Adjusted&#39;, &#39;Segoe UI&#39;, &#39;Liberation Sans&#39;, Roboto, &#39;Helvetica Neue&#39;, Arial, sans-serif;">Aloha from Hawaii</p>

Is there any way to tell Thymeleaf to no escape the quotes but treat them as valid HTML code?

Many thanks for any help

Slevin
  • 617
  • 1
  • 7
  • 17
  • Not a user of Thymeleaf, but, what about `[(${fontFamilySans})]`, based on https://www.thymeleaf.org/doc/tutorials/3.1/usingthymeleaf.html#expression-inlining – Nathan Jul 20 '23 at 00:25
  • 1
    `'` is valid HTML... does it not work in the email client you tested? [https://i.stack.imgur.com/DRxXS.png](https://i.stack.imgur.com/DRxXS.png). – Metroids Jul 20 '23 at 00:31
  • @Nathan: tested, but same result – Slevin Jul 20 '23 at 00:49
  • @Metroids: Unfortunately Thymeleaf renders this quotes as escaped text, so the result in the HTML Source code is this: https://imgur.com/7QrYYk1 – Slevin Jul 20 '23 at 00:49
  • @Metroids: is this really valid HTML code? I know that HTML is built to be as lean as possible, so even when things might work it could be that it's not valid. – Slevin Jul 20 '23 at 00:53
  • Just to be clear: everywhere I would have to use let's say quotes, double-quotes, opening or closing brackets, I always can use the HTML code as well and the browsers will render them correctly? – Slevin Jul 20 '23 at 00:59
  • According to [W3C](https://www.w3.org/International/questions/qa-escapes) -- `You can use a character escape to represent any Unicode character in HTML, XHTML or XML using only ASCII characters.` – Metroids Jul 20 '23 at 04:51
  • Thank you for your help @Metroids. With additionally help of TimRoberts I finally could get some light into my brain and ended up here: https://stackoverflow.com/q/12444605/13609359 . What a ride when you don't know what to ask and where to look at. Anyhow, life is a learning journey... ;) – Slevin Jul 20 '23 at 05:05

0 Answers0