[英]Thymeleaf th:with assignment of a local variable with a condition
我在我的模板中使用了 Thymeleaf。
我有这个问题:我需要使用条件生成title
局部变量。
<title th:with="title=(${firstName} == null and ${lastName} == null) ? 'TITLE A' : 'TITLE B'" th:remove="tag"></title>
<title th:text="${title}"></title>
使用此代码,进入结果模板,我获得<title></title>
Thymeleaf 了解范围。 因此,要解决此问题,您需要将${title}
变量嵌套在<title>
标签之间或同一标签内:
<title th:with="title=(${firstName} == null and ${lastName} == null) ? 'TITLE A' : 'TITLE B'" th:text="${title}">[Title Here]</title>
或者
<title th:with="title=(${firstName} == null and ${lastName} == null) ? 'TITLE A' : 'TITLE B'">
<span th:text="${title}" th:remove="tag">[Title Here]</span>
</title>
但是,您的情况可以简化为:
<title th:text="${firstName} == null and ${lastName} == null ? 'TITLE A' : 'TITLE B'">[Title Here]</title>
我鼓励您在标签之间保留一些默认文本(在本例中为“Title Here”),以便 UI 设计人员无需运行容器即可查看页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.