简体   繁体   English

如何将百里香叶片作为参数传递给另一个片段?

[英]How to pass a thymeleaf fragment as paramter to another fragment?

Is it possible to pass a tag/fragment to another template in thymeleaf ? 是否有可能将标签/片段传递给thymeleaf另一个模板?

Example: I want to create a basic tableview layout, and the callers template should only provide the <tbody> content that then should get injected into the tableview template layout. 示例:我想创建一个基本的tableview布局,调用者模板应该只提供<tbody>内容,然后将其注入tableview模板布局。

This could be the table layout: 这可能是表格布局:

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

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

Calling template: 调用模板:

<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)}"/>

Of course the syntax above is invalid, but you get the idea. 当然上面的语法是无效的,但你明白了。 How could I achieve this? 我怎么能实现这个目标?

It's simple as passing the fragment as id, and nest it below the th:insert tag from calling template: 将片段作为id传递并将其嵌套在调用模板的th:insert标记下面是很简单的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM