java - Thymeleaf th:带有条件的局部变量赋值
问题描述
我在我的模板中使用 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 设计人员无需运行容器即可查看页面。
推荐阅读
- image - 怎么换png
- java - android studio中tensorflow-lite中的GPU委托问题
- php - 是否可以将 add_image_size 与 get_theme_file_uri 一起使用?
- java - 我有两个 id,在将第一个 id 附加到 url 并单击接受按钮后,我需要将第二个 id 附加到 Url 并且无法
- javascript - 在 React 中上传和预览视频
- c# - 为什么JsonResult需要同时实现IActionResult和IStatusCodeActionResult?
- javascript - (Javascript)如何遍历所有 JSON 对象但只获取特定值?
- python - 我如何翻译来自 Django 中的 JSON 文件的静态数据?
- ios - 如何在本机反应中使用缩放的后窗口从下到上为模态设置动画?
- python-3.x - 当 PythonOperator 出现错误“Negsignal.SIGKILL”时,气流 DAG 失败