2
我正在嘗試使用Thymeleaf渲染電子郵件模板,我希望將主題和正文放在同一個文件中,但是單獨渲染。我不想用春景,只是簡單的SpringTemplateEngine
。用Thymeleaf渲染片段內容
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<!--/*@thymesVar id="user" type="com.myapp.user.User"*/-->
<!--/*@thymesVar id="invitationId" type="java.util.UUID"*/-->
<head>
<title th:fragment="subject">Hello <span th:text="${user.name}"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p>
Hello <span th:text="${user.name}"></span>,<br>
you've been invited to join MyApp.
</p>
<p>
Click on the following link to confirm your email and setup a password:<br>
<a th:href="@{https://myapp.com/accept-invitation(id=${invitationId})}">Accept invitation</a>
</p>
<p>
Best regards,<br/>
<em>The MyApp team</em>
</p>
</body>
</html>
有幾個問題與此
首先,我無法獲得Thymeleaf渲染<title>
String subject = templateEngine.process(
templateFile,
ImmutableSet.of("subject"),
new Context(Locale.ENGLISH, contextVariables)
);
只有內容它呈現的文學只有Hello
- 它看起來像開幕<title>
標籤總是被刪除,並且片段在第一個標籤之後立即被修剪。
當我使整個模板,它返回
<!DOCTYPE html>
<html>
<head>
Hello <span>pepa</span></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p>
Hello <span>pepa</span>,<br>
...
使用在<title>
下使輸出更好一點
<span th:text="${user.name}" th:remove="tag"></span>
但開放標籤仍下落不明。
當我理論上能夠正確地呈現標題時,我仍然會將它封裝在一個HTML標記中 - 這顯然不能用作電子郵件主題。有沒有辦法只渲染片段的內容?
我真的很想在同一個模板中同時使用電子郵件主題和正文,例如我想在標題中做一些迭代以從列表生成它 - 我不想做任何字符串串聯,我想在模板中。
有什麼建議嗎?
對不起,但我不認爲你已經理解我的問題,因爲這根本不能幫助我 - 如果是這樣,我不知道如何。 –
我以爲你只需要渲染
是的,沒錯。但是我需要將它輸出的字符串用作電子郵件主題,我不能在主題中放置'