【问题标题】:Conditional text in ThymeLeaf : how to do it in plain text?ThymeLeaf 中的条件文本:如何以纯文本形式进行?
【发布时间】:2015-08-17 11:29:45
【问题描述】:
我了解 th:if 如何适用于 html 模板,但是当您期望纯文本时,我没有找到任何线索(用例:纯文本电子邮件模板)。
到目前为止我试过了:
<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
Dear [[${contact.firstname}]] [[${contact.lastname}]],
An alert was triggered at location: [[${account.address}]]
<span th:if=\"${videoLink}\">To view your security camera recordings, please click on [[${videoLink]]</span>
</html>
它可以工作......但结果包含标签。知道我做错了什么吗?
谢谢,
西里尔
【问题讨论】:
标签:
if-statement
thymeleaf
plaintext
【解决方案1】:
Thymeleaf 2.1 有一个th:block 标签,它基本上是一个属性容器。您的条件文本可以这样完成:
<th:block th:if="${videoLink}">To view your...</th:block>
【解决方案2】:
似乎与 th:inline 不同,th:remove="" 并不应用于子节点,并且必须为每个标签添加。在这里,如果我将它添加到标签中,结果就是我想要的:
<html xmlns:th="http://www.thymeleaf.org" th:inline="text" th:remove="tag">
Dear [[${contact.firstname}]] [[${contact.lastname}]],
An alert was triggered at location: [[${account.address}]]
<span th:if=\"${videoLink}\" th:remove="tag">To view your security camera recordings, please click on [[${videoLink]]</span>
</html>
结果 :
Dear John Doe,
An alert was triggered at localation: 205 North Michigan Avenue Chicago, IL
To view your security camera recordings, please click on http://www.video.com?id=007