Thymeleaf 插入頁面片段屬性th:insert、th:replace、th:include差別如下。
th:insert:把片段插入所屬標籤中。th:replace:把片段取代所屬標籤。th:include:把片段內容插入所屬標籤中(3.0開始不建議使用)。
例如要插入的th:fragment="header"片段在header.html如下。
header.html
<header th:fragment="header">
<h1>Thymeleaf Basic Include Demo</h1>
</header>
使用th:insert插入片段。
<div th:insert="~{header :: header}">Header</div>
th:insert顯示結果。片段插入所屬標籤中。
<div>
<header>
<h1>Thymeleaf Basic Include Demo</h1>
</header>
</div>
使用th:replace插入片段。
<div th:replace="~{header :: header}">Header</div>
th:replace顯示結果。片段取代所屬標籤。
<header>
<h1>Thymeleaf Basic Include Demo</h1>
</header>
使用th:include插入片段。
<div th:include="~{header :: header}">Header</div>
th:include顯示結果。片段內容插入所屬標籤中。
<div>
<h1>Thymeleaf Basic Include Demo</h1>
</div>
沒有留言:
張貼留言