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.
It works for HTML, but for plain text variables in the common parts are not replaced:
HTML
common.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div th:fragment="header"> <p> Hello, [( ${name} )] </p> </div> <div th:fragment="footer"> <p> Bye. </p> </div> </body> </html>
specific.html
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <div th:replace="html/common::header"></div> <p> <a th:href="${myLink}">[( ${myLink} )]</a> </p> <div th:replace="html/common::footer"></div> </body> </html>
Plain text
header.txt
Hello ${name}
footer.txt
Bye
specific.txt
[( ~{text/header} )] [( ${myLink} )] [( ~{text/footer} )]
Result
It all works well for HTML but for the plain text version the ${name}
variable from the inserted header.txt
template is not replaced:
Hello, [#th:block th:utext="${name}"][/th:block]
http://example.com
Bye.
The HTML result looks correct:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<p>
Hello, name-value
</p>
</div>
<p>
<a href="http://example.com">http://example.com</a>
</p>
<div>
<p>
Bye.
</p>
</div>
</body>
</html>
My questions
- Is there an elegant solution for the plain text version?
- Is there a way to define and use fragments also for textual Thymeleaf templates?
- Any general recommendations, as I'm only starting to use Thymeleaf?