【问题标题】:Is it possible to pass parameters to layout in thymeleaf layout-dialect?是否可以将参数传递给百里香布局方言中的布局?
【发布时间】:2015-04-22 13:25:45
【问题描述】:

我有一个通用布局,默认情况下,应该在每个页面上显示一个(基本)搜索表单,但搜索页面本身已经包含一个(更高级)搜索表单。

是否可以将我的搜索页面中的参数传递给布局以不显示默认搜索表单?

这是我想做的一个例子:

layout.html

<html layout:???="displayShowForm = true">
    ...
    <form action="search" th:if="${displayShowForm}">...</form>
    ...
    <div layout:fragment="content">...</div>

home.html(显示默认搜索表单)

<html layout:decorator="layout">
    ...
    <div layout:fragment="content">...</div>

search.html(隐藏默认搜索表单)

<html layout:decorator="layout (displayShowForm = false)">
    ...
    <div layout:fragment="content">
        ...
        <form action="advancedSearch">...</form>

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

    是的,这完全有可能,尽管 Thymeleaf 的文档没有明确说明。

    您所要做的就是使用 th:with 属性传递您的参数。可能还有其他方法,但这似乎是最直接的。

    这是我的实现的精简版:

    默认装饰器 - 片段/布局/default.html

    <!doctype html>
    <html xmlns:layout="http://www.thymeleaf.org" xmlns:th="http://www.thymeleaf.org">
    <body>
      <div th:replace="fragments/header :: main"></div>
      <div layout:fragment="content">
        main content goes here
      </div>
    </body>
    </html>
    

    标题片段 - 片段/header.html

    <!doctype html>
    <html xmlns:th="http://www.thymeleaf.org">
    <body>
      <div th:fragment="main">
        <nav>
          <ul>
            <li><a href="#" th:classappend="${currentPage == 'home'} ? 'active'">Home Page</a></li>
            <li><a href="#" th:classappend="${currentPage == 'about'} ? 'active'">About</a></li>
          </ul>
        </nav>
      </div>
    </body>
    

    主页文件 - home.html

    <!doctype html>
    <html layout:decorator="layout/default" th:with="currentPage='home'"
      xmlns:layout="http://www.thymeleaf.org/" xmlns:th="http://www.thymeleaf.org">
    <body>
      <div layout:fragment="content">
        This is my home page content... thrilling, isn't it?
      </div>
    </body>
    </html>
    

    在这里,在 home.html 文件中,您可以看到我包含默认装饰器并使用 th:with 属性传递我的参数。我实际上并没有在我的布局装饰器中使用我的参数,但我在 header.html 中使用它,它包含在装饰器中。无需将它从装饰器传递到 header.html 片段,因为它已经在范围内。

    也不需要对 header.html 中的 currentPage 变量进行 NULL 检查。从 home.html 中删除参数时,没有附加活动的 CSS 类。

    如果我要渲染 home.html,我希望看到以下输出:

    <!doctype html>
    <html>
    <body>
      <nav>
        <ul>
          <li><a href="#" class="active">Home Page</a></li>
          <li><a href="#">About</a></li>
        </ul>
      </nav>
      <div>
        This is my home page content... thrilling, isn't it?
      </div>
    </body>
    </html>
    

    【讨论】:

    【解决方案2】:

    是的,可以传递参数,但您需要使用layout:include 而不是layout:decoratorlayout:fragment

    类似于 Thymeleaf 的 th:include,但允许传递整个 元素片段到包含的页面。如果你有一些 HTML 很有用 想要重用,但内容过于复杂 仅使用上下文变量来确定或构造。

    来源:https://github.com/ultraq/thymeleaf-layout-dialect

    您应该看看this documentation,它会为您提供有关使用方法的详细信息。

    在你的情况下,它可能看起来像:

    <div layout:include="form" th:with="displayShowForm=true"></div>
    

    并且在form的布局页面中:

    <div layout:fragment="form">
        <div th:if="${displayShowForm} == true">
            <form action="basicSearch"></form>
        </div>
        <div th:if="${displayShowForm} == false">
            <form action="advancedSearch"></form>
        </div>
    </div>
    

    【讨论】:

    • 使用 layout:include 代替 layout:decorator 的问题是标题没有像在 layout:decorator 中那样被解析。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    相关资源
    最近更新 更多