是否可以将参数传递给 thymeleaf 布局方言中的布局? [英] Is it possible to pass parameters to layout in thymeleaf layout-dialect?

查看:28
本文介绍了是否可以将参数传递给 thymeleaf 布局方言中的布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我想做的一个例子:

layout.html

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

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

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

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

...<div layout:fragment="content">...<form action="advancedSearch">...</form>

解决方案

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

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

这是我的实现的精简版本:

默认装饰器 - fragment/layout/default.html

<html xmlns:layout="http://www.thymeleaf.org" xmlns:th="http://www.thymeleaf.org"><身体><div th:replace="fragments/header::main"></div><div layout:fragment="content">主要内容在这里

</html>

标题片段 - fragment/header.html

<html xmlns:th="http://www.thymeleaf.org"><身体><div th:fragment="main"><导航><ul><li><a href="#" th:classappend="${currentPage == 'home'} ? 'active'">主页</a></li><li><a href="#" th:classappend="${currentPage == 'about'} ? 'active'">关于</a></li></nav>

主页文件 - home.html

<html layout:decorator="layout/default" th:with="currentPage='home'"xmlns:layout="http://www.thymeleaf.org/" xmlns:th="http://www.thymeleaf.org"><身体><div layout:fragment="content">这是我的主页内容……令人兴奋,不是吗?

</html>

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

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

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

<身体><导航><ul><li><a href="#" class="active">主页</a></li><li><a href="#">关于</a></li></nav><div>这是我的主页内容……令人兴奋,不是吗?

</html>

I have a common layout which, by default, should display a (basic) search form on each page excepted the search page itself which contains a (more advanced) search form already.

Is it possible to pass a parameter from my search page to the layout in order to not display the default search form?

Here is an example of what I would like to do:

layout.html

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

home.html (show the default search form)

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

search.html (hide the default search form)

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

解决方案

Yes, it's entirely possible, even though Thymeleaf's documentation doesn't clearly state it.

All you have to do is pass your param using the th:with attribute. There may be other methods, but this seems to be the most straight-forward.

Here's a stripped down version of my implementation:

Default decorator - 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 fragment - 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 Page file - 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>

Here, in the home.html file, you can see I include the default decorator and pass my parameter using the th:with attribute. I don't actually use my parameter in my layout decorator, but I use it in header.html, which is included from the decorator. No need to pass it from the decorator to the header.html fragment, since it's already in scope.

There was also no need to do a NULL check on the currentPage variable in header.html. The active CSS class was simply not appended when removing the parameter from home.html.

If I were to render home.html, I would expect to see the following output:

<!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>

这篇关于是否可以将参数传递给 thymeleaf 布局方言中的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆