thymeleaf公共页面元素抽取

一、抽取公共片断

  使用thymeleaf的 th:fragment 为样抽取的公共片断命名,html

  以下把div标签命名为 copy,就能够获取到div整个里的内容ide

  <div th:fragment="copy">
    &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">
    &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>作一些自定义操做

相关文章
相关标签/搜索