title: Define and insert Thymeleaf fragments in TEXT templates
body: <p>I'm trying to create email templates in both plain text and HTML with Thymeleaf. Because I don't want to duplicate the common parts I want to define these parts separately and insert them into the more specific templates.</p>
  
  <p>It works for HTML, but for plain text variables in the common parts are not replaced:</p>
  
  <p><strong>HTML</strong></p>
  
  <ul>
  <li><p>common.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div th:fragment="header"&gt;
              &lt;p&gt;
                  Hello, [( ${name} )]
              &lt;/p&gt;
          &lt;/div&gt;
          &lt;div th:fragment="footer"&gt;
              &lt;p&gt;
                  Bye.
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre></li>
  <li><p>specific.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html xmlns:th="http://www.thymeleaf.org"&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div th:replace="html/common::header"&gt;&lt;/div&gt;
          &lt;p&gt;
              &lt;a th:href="${myLink}"&gt;[( ${myLink} )]&lt;/a&gt;
          &lt;/p&gt;
          &lt;div th:replace="html/common::footer"&gt;&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre></li>
  </ul>
  
  <p><strong>Plain text</strong></p>
  
  <ul>
  <li><p>header.txt</p>
  
  <pre><code>Hello ${name}
  </code></pre></li>
  <li><p>footer.txt</p>
  
  <pre><code>Bye
  </code></pre></li>
  <li><p>specific.txt</p>
  
  <pre><code>[( ~{text/header} )]
  [( ${myLink} )]
  [( ~{text/footer} )]
  </code></pre></li>
  </ul>
  
  <p><strong>Result</strong></p>
  
  <p>It all works well for HTML but for the plain text version the <code>${name}</code> variable from the inserted <code>header.txt</code> template is not replaced:</p>
  
  <pre><code>Hello, [#th:block th:utext="${name}"][/th:block]
  
      http://example.com
  
  Bye.
  </code></pre>
  
  <p>The HTML result looks correct:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;div&gt;
              &lt;p&gt;
                  Hello, name-value
              &lt;/p&gt;
          &lt;/div&gt;
          &lt;p&gt;
              &lt;a href="http://example.com"&gt;http://example.com&lt;/a&gt;
          &lt;/p&gt;
          &lt;div&gt;
              &lt;p&gt;
                  Bye.
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>My questions</strong></p>
  
  <ul>
  <li>Is there an elegant solution for the plain text version?</li>
  <li>Is there a way to define and use fragments also for textual Thymeleaf templates?</li>
  <li>Any general recommendations, as I'm only starting to use Thymeleaf?</li>
  </ul>