一.包括模板片断:html
1:定义和引用片断,咱们常常会想要包含在模板片断来自其余模板。常见的用途是页脚、标题、菜单…;dom
为了作到这一点,Thymeleaf须要咱们定义包含可用的片断,咱们能够经过使用th:fragment属性。spa
定义一个页面底部footer页面,在每个须要的页面均可以用的模板,能够经过使用th:fragment属性code
<div th:fragment="copy"> © 2014 The Good Thymes Virtual Grocery </div>
上面的代码定义了一个叫作副本的片断,咱们能够很容易地包含在咱们的主页上经过使用th:include
or th:replace属性
之一:orm
<body> ... <div th:include="footer :: copy"></div> </body>
引用片断没有th:fragment:htm
... <div id="copy-section"> © 2011 The Good Thymes Virtual Grocery </div> ...
页面引用:th:include="templatename::domselector"blog
templatename是要引入页面的路劲加上去掉后缀的名称,例如footer.html页面创建在/WEB-INF/templates/footer.html,因此templatename为footer;domselector就是dom选择器,即为th:fragment中的值,或是选择id
<body> ... <div th:include="footer :: #copy-section"></div> </body
注意:it
带有公共的页面,不要带有 <html> <head></head> <body></body> </html> 直接写内容: <div th:fragment="copy"> © 2011 The Good Thymes Virtual Grocery </div>
扩展写法,但愿能灵活运用:io
<div th:include="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>
二.可参数化的片断签名模板
能够像参数同样传入参数:
<div th:fragment="frag (onevar,twovar)"> <p th:text="${onevar} + ' - ' + ${twovar}">...</p> </div>
两种调用方式引入页面:
<div th:include="::frag (${value1},${value2})">...</div> <div th:include="::frag (onevar=${value1},twovar=${value2})">...</div>
若是没有带参数,以下形式:
<div th:fragment="frag"> ... </div>
依然可使用带参数的引入,可是必须使用第二种引入方式,另外一种不行:以下是正确的引入方式
<div th:include="::frag (onevar=${value1},twovar=${value2})">
这样事实上,这将是至关于一个th:include
和th:with的
组合
<div th:include="::frag" th:with="onevar=${value1},twovar=${value2}">