【问题标题】:How to pass a thymeleaf fragment as paramter to another fragment?如何将百里香片段作为参数传递给另一个片段?
【发布时间】:2019-06-12 10:07:08
【问题描述】:

是否可以将标签/片段传递给thymeleaf 中的另一个模板?

示例:我想创建一个基本的tableview 布局,调用者模板应该只提供<tbody> 内容,然后应该注入到tableview 模板布局中。

这可能是表格布局:

<div th:fragment="tableview (tbodyFragment)">
    <table class=...>
        <thead>...</thead>

        <!-- the table body should be repaced -->
        <tbody th:replace="${tbodyFragment}"/>
    </table>
</div>

调用模板:

<tbody id="tbodyFragment">
    <th:block th:each="row : ${rows}">
        <tr>
            <td th:text="${row.id}"/>
            <td th:text="${row.firstname}"/>
            <td th:text="${row.lastname}"/>
            <td th:text="${row.age}" style="text-align:right"/>
        </tr>   
    <th:block>
</tbody>

<div th:insert="~{tableview::tableview(tbodyFragment)}"/>

当然上面的语法是无效的,但你明白了。我怎样才能做到这一点?

【问题讨论】:

    标签: java thymeleaf


    【解决方案1】:

    很简单,将片段作为id传递,并将其嵌套在调用模板的th:insert标签下方:

    <div th:insert="~{tableview::tableview(~{:: #tbodyFragment})}">
       <tbody id="tbodyFragment">
               ...content here...
       </tbody>
    </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多