【发布时间】:2020-04-24 10:36:09
【问题描述】:
我正在尝试使用 Thymeleaf 创建纯文本和 HTML 格式的电子邮件模板。因为我不想复制公共部分,所以我想分别定义这些部分并将它们插入到更具体的模板中。
它适用于 HTML,但对于普通部分中的纯文本变量不会被替换:
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> -
具体的.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>
纯文本
-
header.txt
Hello ${name} -
页脚.txt
Bye -
具体的.txt
[( ~{text/header} )] [( ${myLink} )] [( ~{text/footer} )]
结果
这一切都适用于 HTML,但对于纯文本版本,插入的 header.txt 模板中的 ${name} 变量不会被替换:
Hello, [#th:block th:utext="${name}"][/th:block]
http://example.com
Bye.
HTML 结果看起来正确:
<!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>
我的问题
- 纯文本版本是否有优雅的解决方案?
- 有没有办法为文本 Thymeleaf 模板定义和使用片段?
- 任何一般性建议,因为我才开始使用 Thymeleaf?
【问题讨论】:
标签: thymeleaf