在本文中,我们将展示如何在Thymeleaf 模板中使用片段。表示可包含在其他模板中的模板片段的片段。它可以是页眉,菜单,页脚和文档的任何其他部分,通常在许多页面上重复。片段可以参数化并包含在特定的初始参数中。
如果您需要有关如何在弹簧启动应用程序中安装和使用百里香的更多信息,请点击以下链接:使用百里香的弹簧启动。
为了定义新的百里叶碎片,我们使用属性。片段可以隔离在单独的文件中(我们建议使用此方法),也可以在任何其他模板中定义。th:fragment
最简单的片段可以具有以下结构:
- <footer th:fragment="footer">
- <p>2019 FrontBackend.com</p>
- </footer>
我们还可以在单个HTML文档中定义多个片段,也可以是格式正确的HTML文件:
- <!DOCTYPE HTML>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8"/>
- <title>Spring Boot Thymeleaf Application - Fragments</title>
- </head>
- <body>
-
- <div th:fragment="header">
- <h1>Thymeleaf Fragments</h1>
- </div>
-
- <div th:fragment="menu">
- <a href="#home" class="active">Home</a>
- <a href="#blog">Blog</a>
- <a href="#contact">Contact</a>
- <a href="#about">About</a>
- </div>
-
- <footer th:fragment="footer">
- <p>2019 FrontBackend.com</p>
- </footer>
-
- </body>
- </html>
有三种方法可以包含片段中的内容:
在以下示例中,我们包含使用三种方法的片段:footerparts
- <body>
-
- <div th:insert="parts :: footer"></div>
-
- <div th:replace="parts :: footer"></div>
-
- <div th:include="parts :: footer"></div>
-
- </body>
结果页面将具有以下结构:
- <body>
-
- <div>
- <footer>
- <p>2019 FrontBackend.com</p>
- </footer>
- </div>
-
- <footer>
- <p>2019 FrontBackend.com</p>
- </footer>
-
- <div>
- <p>2019 FrontBackend.com</p>
- </div>
-
- </body>
百里叶碎片可以使用简单的DOM选择器包含在另一个模板中。我们可以使用 HTML 元素的 id 或类名来获取它们。在下面的示例中,我们包含四个片段,其中一个使用 DIV 元素的类名:div.panel
- <!DOCTYPE HTML>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8"/>
- <title>Spring Boot Thymeleaf Application - Fragments Main</title>
- </head>
- <body>
-
- <header th:insert="_parts :: header"> </header>
- <div th:replace="_parts :: menu"></div>
- <div th:replace="_parts :: div.panel"></div>
- <div th:replace="_parts :: footer"></div>
-
- </body>
- </html>
结果将具有以下结构:
-
- <!DOCTYPE HTML>
- <html lang="en">
- <head>
- <meta charset="UTF-8"/>
- <title>Spring Boot Thymeleaf Application - Fragments Main</title>
- </head>
- <body>
-
- <header>
- <div>
- <h1>Thymeleaf Fragments</h1>
- </div>
- </header>
- <div>
- <a href="#home" class="active">Home</a>
- <a href="#blog">Blog</a>
- <a href="#contact">Contact</a>
- <a href="#about">About</a>
- </div>
- <div class="panel">
- This is a sample panel
- </div>
- <footer>
- <p>2019 FrontBackend.com</p>
- </footer>
-
- </body>
- </html>
定义 与 的片段可以指定一组输入参数。要使用参数化片段,我们需要提供一个片段名称和声明的参数列表。th:fragment
- <div th:fragment="fragmentName(parameter1, parameter2 ...)">
- ...
- </div>
在下面的示例中,我们定义了一个负责在 HTML 表中显示单个联系人行的片段。
- <!DOCTYPE HTML>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8"/>
- <title>Spring Boot Thymeleaf Application - Fragments Tables</title>
- </head>
- <body>
-
- <table>
- <tr th:fragment="contactRow(stat, row)">
- <td th:text="${stat.count}">1</td>
- <td th:text="${row.name}">Name</td>
- <td th:text="${row.email}">Email</td>
- <td th:text="${row.type.name() == 'INDIVIDUAL' ? 'Individual' : 'Group'}"></td>
- <td>
- <th:block th:if="${row.type.name() == 'GROUP'}">
- <p th:each="member : ${row.members}" th:text="${member.email}"></p>
- </th:block>
- </td>
- </tr>
- </table>
-
- </body>
- </html>
我们使用属性在联系人列表中包含参数化片段:th:replacecontactRow(stat, row)
- <!DOCTYPE HTML>
- <html lang="en" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8"/>
- <title>Spring Boot Thymeleaf Application - Fragments - Contacts</title>
- </head>
- <body>
-
- <table cellspacing="1" cellpadding="1" border="1">
- <thead>
- <tr>
- <th>No</th>
- <th>Name</th>
- <th>Email</th>
- <th>Type</th>
- <th>Members</th>
- </tr>
- </thead>
- <tbody>
- <tr th:each="contact, stat : ${contacts}">
- <th:block th:replace="_tables :: contactRow(${stat}, ${contact})">row</th:block>
- </tr>
- </tbody>
- </table>
-
- </body>
- </html>
参数化片段为我们提供了很多可能性。它允许我们以许多不同的方式重用一个模板。我们还可以将常用的逻辑分离到一个片段中,并将其附加到许多模板中。
片段表达式不仅可以用于对象,数字,列表等参数,还可以提供标记片段。
假设我们希望在标头部分中定义一组样式表和javascript,但也有可能使用其他资源扩展该列表。
基本标头片段将具有以下结构:
- <head th:fragment="baseHeader(title,links)">
-
- <title th:replace="${title}">Base Title | FrontBackend</title>
-
- <!-- Common styles and scripts -->
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
- <link rel="shortcut icon" th:href="@{/images/favicon.ico}">
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.js"></script>
-
- <!--/* This is a laceholder for additional links */-->
- <th:block th:replace="${links}" />
-
- </head>
现在,如果我们想在标题中加入一些额外的链接,只需使用以下示例:baseHeader
- <head th:replace="base :: baseHeader(~{::title},~{::link})">
-
- <title>The best tutorial ever | FrontBackend</title>
-
- <link rel="stylesheet" th:href="@{/css/awesome.min.css}">
- <link rel="stylesheet" th:href="@{/jquery/jquery-ui.css}">
-
- </head>
::title并指向当前模板中的标题和链接元素。结果头标记将如下所示:::link
- <head>
-
- <title>The best tutorial ever | FrontBackend</title>
-
- <!-- Common styles and scripts -->
- <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"/>
- <link rel="shortcut icon" th:href="@{/images/favicon.ico}">
- <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.slim.js"></script>
-
- <link rel="stylesheet" th:href="@{/css/awesome.min.css}">
- <link rel="stylesheet" th:href="@{/jquery/jquery-ui.css}">
-
- </head>
在本文中,我们已经展示了百里叶中的模板布局机制是多么灵活。我们介绍了如何重用片段以及如何使用参数化模板。所有功能使片段成为模板管理的高级工具。