使用thymeleaf的 th:fragment 爲樣抽取的公共片斷命名,html
以下把div標籤命名爲 copy,就能夠獲取到div整個裏的內容ide
<div th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</div>htm
<div th:insert="~{footer :: copy}"></div>
~{templatename::selector}:模板名::選擇器
~{templatename::fragmentname}:模板名::片斷名模板
insert的公共片斷在div標籤中class
若是使用th:insert等屬性進行引入,能夠不用寫~{}:
行內寫法能夠加上:[[~{}]];[(~{})];select
三種引入公共片斷的th屬性:fragment
th:insert:將公共片斷整個插入到聲明引入的元素中命名
th:replace:將聲明引入的元素替換爲公共片斷di
th:include:將被引入的片斷的內容包含進這個標籤中thymeleaf
<footer th:fragment="copy">
© 2011 The Good Thymes Virtual Grocery
</footer>
引入方式
<div th:insert="footer :: copy"></div>
<div th:replace="footer :: copy"></div>
<div th:include="footer :: copy"></div>
效果
<div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
</div>
<footer>
© 2011 The Good Thymes Virtual Grocery
</footer>
<div>
© 2011 The Good Thymes Virtual Grocery
</div>
引入片斷的時候傳入參數:
<nav class="col-md-2 d-none d-md-block bg-light sidebar" id="sidebar">
<a class="nav-link active" th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}" href="#" th:href="@{/main.html}"></a>
</nav>
<!--引入側邊欄;傳入參數-->
<div th:replace="commons/bar::#sidebar(activeUri='emps')"></div>
而後能夠經過
<a class="nav-link active" th:class="${activeUri=='main.html'?'nav-link active':'nav-link'}" href="#" th:href="@{/main.html}"> </a>
像上面的<a>作一些自定義操做