Thymeleaf 中的标题和标题 [英] Head and Title in Thymeleaf

查看:54
本文介绍了Thymeleaf 中的标题和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Thymeleaf 初学者.我从一个常见的布局页面开始:

fragments/layout.html

<html xmlns:th="http://www.thymeleaf.org"><head th:fragment="headerFragment"><title>模板标题</title><!-- 元数据、链接和脚本--><身体><div class="容器">一些文字

</html>

还有一个内容页面:

page.html

<html xmlns:th="http://www.thymeleaf.org"><head th:include="fragments/layout::headerFragment"><title>页面标题</title><!-- 元数据、链接和脚本--><身体><div class="容器">一些文字

</html>

当我渲染页面时,标题总是来自模板,而不是来自页面.在 Thymeleaf 中是否有可能有共同的元数据、脚本和样式(在 HEAD 标签中)但有一个每页标题?

解决方案

我也遇到了这个问题(谢谢nmy 参考文档!)这里是我注意到了什么以及我如何在我的应用程序中解决它:

文档中需要注意的事项:

  1. th:includeth:replace
  2. 的区别
  3. 通过 domselector 而不是通过 th:fragment
  4. 引用片段
  5. Thymeleaf 提供了一个this"选项来查找选择器

考虑到这 3 件事,您可以执行以下操作:

fragments/layout.html:

<title th:include=":: #pageTitle">布局通用标题</标题><!-- 元数据、链接和脚本-->

page.html

<title id="pageTitle">页面标题</title><!-- 您想在布局中引用的其他元素-->

希望这会有所帮助!

I'm a Thymeleaf beginner. I started with a common layout page:

fragments/layout.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerFragment">
    <title>Template title</title>
    <!-- metas, link and scripts -->
</head>
<body>
<div class="container">
    Some text
</div>
</body>
</html>

And a content page:

page.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/layout :: headerFragment">
    <title>Page title</title>
    <!-- metas, link and scripts -->
</head>
<body>
<div class="container">
    Some text
</div>
</body>
</html>

When I render the page, the title is always from the template, not from the page. Is it possible in Thymeleaf to have metas, scripts and style in common (in the HEAD tag) but to have a per-page title?

解决方案

I was also having this problem (Thank you nmy for referencing the documentation!) Here is what I noticed and how I solved it in my app:

Things to note from the documentation:

  1. The differences between th:include and th:replace
  2. Referencing fragments by domselector instead of by th:fragment
  3. Thymeleaf provides a "this" option for finding selectors

With these 3 things in mind, you can do the following:

fragments/layout.html:

<head th:fragment="headerFragment">
    <title th:include=":: #pageTitle">Layout Generic Title< /title>
    <!-- metas, link and scripts -->
</head>

page.html

<head th:include="fragments/layout :: headerFragment">
    <title id="pageTitle">Page title</title>
    <!-- other elements you want to reference in your layout -->
</head>

Hope this helps!

这篇关于Thymeleaf 中的标题和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆