是否可以将参数传递给百里香布局方言中的布局?

Is it possible to pass parameters to layout in thymeleaf layout-dialect?

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

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

这是我想做的一个例子:

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>

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

Similar to Thymeleaf's th:include, but allows the passing of entire element fragments to the included page. Useful if you have some HTML that you want to reuse, but whose contents are too complex to determine or construct with context variables alone.

来源: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>

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

您所要做的就是使用 th:with 属性传递您的参数。可能还有其他方法,但是这个好像是最多的straight-forward.

这是我的实施的精简版:

默认装饰器 - fragments/layout/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 片段 - fragments/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 class

如果我要渲染 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>