一、参考资料
二、实际测试栗子(文档中都有,只列举一个栗子)
片段定义
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
</body>
</html>
引入定义
<body>
...
<div th:insert="~{footer :: copy}"></div>
</body>
这里有个小细节:
th:insert="~{footer :: copy}"中的footer,如果文件不同级,需要写详细路径,如:th:insert="~{view/footer :: copy}";
三、传值问题
定义片段中接收变量和传值
片段1:
<div th:fragment="frag (onevar,twovar)">
<p th:text="${onevar} + ' - ' + ${twovar}">...</p>
</div>
引入传值页1:
<!-- 无需纠结是replace还是include、insert -->
<div th:replace="::frag (${value1},${value2})">...</div>
当片段中未定义有关形参时:
片段2:
<aside class="main-sidebar">
<span th:text="${tree == 'index'}?'6666':'9999'"></span>
<span th:text="${treemenu}"></span>
</aside>
引入传值页面2:
<aside th:include="~{view/sidebar}" th:with="tree=${'index'},treemenu=${'overView'}"></aside>
使用的th:with标签携带数据