thymeleaf公共頁面元素抽取


1、抽取公共片段

  使用thymeleaf的 th:fragment 為樣抽取的公共片段命名,

  如下把div標簽命名為 copy,就可以獲取到div整個里的內容

  <div th:fragment="copy">
    &copy; 2011 The Good Thymes Virtual Grocery
  </div>

2、引入公共片段

  <div th:insert="~{footer :: copy}"></div>
  ~{templatename::selector}:模板名::選擇器
  ~{templatename::fragmentname}:模板名::片段名

3、默認效果:

  insert的公共片段在div標簽中

  如果使用th:insert等屬性進行引入,可以不用寫~{}:
  行內寫法可以加上:[[~{}]];[(~{})];

  三種引入公共片段的th屬性:

    th:insert:將公共片段整個插入到聲明引入的元素中

    th:replace:將聲明引入的元素替換為公共片段

    th:include:將被引入的片段的內容包含進這個標簽中

  <footer th:fragment="copy">
    &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>
      &copy; 2011 The Good Thymes Virtual Grocery
    </footer>
  </div>

  <footer>
    &copy; 2011 The Good Thymes Virtual Grocery
  </footer>

  <div>
    &copy; 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>做一些自定義操作


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM