Use可以使用Thymeleaf Ultraq Layout创建一个基本模板,该模板将充当其他模板的装饰器,如下所示:
base-template.html:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"><head> <title layout:title-pattern="$CONTENT_TITLE - $LAYOUT_TITLE">Sample</title> <meta name="description" content=""/> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <!-- all CSS links here --></head><body><div > <div > <div layout:fragment="page_content"> <!-- Content from other pages which decorate using this template --> </div> </div></div><!-- /.container --><!-- All script tags here --><th:block layout:fragment="scripts"> <!-- If you have any page specific scripts --></th:block></body></html>
然后其他页面将使用上面的模板作为装饰器,如下所示:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{base-template}"><head> <title>This page title</title></head><div layout:fragment="page_content"> <!-- content for this page --></div><th:block layout:fragment="scripts"> <!-- add any scripts related to this page --></th:block></html>语法从
~{base-template}Thymeleaf 3开始使用。您可以继续上述方法,而不必在其他页面上重复导航,页眉和页脚。



