Layout布局
1 定义fragment
语法: th:fragement
示例:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>
</body>
</html>
fragmeng使用示例:
<body>
...
<div th:insert="~{footer :: copy}"></div>
<div th:insert="footer :: copy"></div>
</body>
下面是一个动态的模板fragment示例:
<div th:insert="footer :: (${user.isAdmin}? #{footer.admin} : #{footer.normaluser})"></div>
上述示例中,功能强大,可以实现动态的fragment替换。
th:replace vs th:insert
insert方法会在fragment外层创建一个div,而replace则不会。
示例如下:
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
输出的结果信息如下:
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<!-- replace output -->
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
comments
示例1:
示例2,中间的代码将被parser忽略.
<!--/*-->
<div>
you can see me only before Thymeleaf processes me!
</div>
<!--*/-->
示例3:
<!--/*/ <th:block th:each="user : ${users}"> /*/-->
中间的内容在浏览器中展示之时,将自动被移除掉