创建一个thymelaf片段列表,并将其作为param附加到另一个片段



是否可以静态初始化片段列表并将其作为参数传递给另一个片段?类似这样的东西:

这个:

<div th:fragment="links(name)" xmlns:th="http://www.w3.org/1999/html" th:remove="tag">
<span th:replace="~{fthis::htmlIconTemplate(${name}, '')}"/>
</div>
<th:block th:replace="fragments/new/template1 :: standartTemplate('', ${content}, { {{~{this::links('live')}, ~{this::links('live')}}})">
</th:block>

模板1:

<tr th:fragment="standartTemplate(mode, content, customFragments)" xmlns:th="http://www.w3.org/1999/xhtml">
<th:block th:replace="${customFragments.get(1)}"/>
</tr>

我不确定这是否完全回答了您的问题。在下面的例子中,解决方案的核心是这一行:

<th:block th:replace = "__${#strings.listSplit(customFragments, ',')[1]}__">

该示例从更简单的案例开始构建。因为我没有你的代码的一些更广泛的上下文,当然,这必须根据你的具体情况进行调整。

设置

frags_demo.html的内容-放在resources文件夹中:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Fragments Demo</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>Fragments Demo</h2>
<h3>In-page fragment:</h3>
<div th:fragment="links(name)" th:remove="tag">
<div th:text="'Here is ' + ${name}"></div>
</div>
<h3>First example:</h3>
<!-- simple example, with a string not fragment names -->
<th:block th:replace = "/fragments/fragments_one.html :: fragment_a(
mode = 'a mode', 
content = ${content}, 
customFragments = 'foo,bar,baz')">
</th:block>
<h3>Second example:</h3>
<!-- example with hard-coded fragment names -->
<th:block th:replace = "/fragments/fragments_one.html :: fragment_a(
mode = 'a mode', 
content = ${content}, 
customFragments = '/frags_demo.html::links('foo'),bar,baz')">
</th:block>
<h3>Third example:</h3>
<!-- example with hard-coded fragment names, using the template fragment -->
<!-- use the absolute fragment path & name - can't use 'this'            -->
<th:block th:replace = "/fragments/fragments_one.html :: fragment_b(
mode = 'a mode', 
content = ${content}, 
customFragments = '/frags_demo.html::links('foo'),
/frags_demo.html::links('bar'),
/frags_demo.html::links('baz')')">
</th:block>
</body>
</html>

fragments_one.html的内容-放置在resources文件夹下的fragments子文件夹中:

<!-- the customFragments parameter contains the string 'foo,bar,baz' -->
<th:block th:fragment="fragment_a(mode, content, customFragments)">
<th:block th:text="${'Fragment A from fragments_one.html'}">
</th:block>
<!-- use the Thymeleaf listSplit function to create a list -->
<!-- giving us ['foo', 'bar', 'baz']                       -->
<!-- picking one of the items (the 2nd one - zero-indexed!): -->
<div th:with="frag=${#strings.listSplit(customFragments, ',')[1]}"
th:text="${frag}">
</div>
<!-- iterating through the entire list: -->
<div th:each="frag : ${#strings.listSplit(customFragments, ',')}">
<div th:text="${frag}"></div>
</div>
</th:block>
<th:block th:fragment="fragment_b(mode, content, customFragments)">
<div>The string representing the template:</div>
<div th:with="frag=${#strings.listSplit(customFragments, ',')[1]}"
th:text="${frag}">
</div>
<p/>
<div>The invoked template:</div>
<th:block th:replace = "__${#strings.listSplit(customFragments, ',')[1]}__">
</th:block>
</th:block>

输出

当您点击frags_demo页面时,浏览器中会显示以下内容:

Fragments Demo
In-page fragment:
Here is null
First example:
Fragment A from fragments_one.html
bar
foo
bar
baz
Second example:
Fragment A from fragments_one.html
bar
/frags_demo.html::links('foo')
bar
baz
Third example:
The string representing the template:
/frags_demo.html::links('bar')
The invoked template:
Here is bar

最后一行(here is bar(表示参数化片段调用的结果。

演练

我通过这些例子来增加复杂性,以帮助澄清事情是如何运作的。需要注意的要点是:

Thymelaf将每个片段参数作为字符串传递。因此,我们使用内置的#strings.listSplit函数来给我们一个可迭代的列表。在我的示例中,它是逗号分隔的(foo,bar,baz(。根据字符串中的结束内容,逗号可能不是适合您的分隔符。

如果您不想在整个列表中进行迭代,可以使用[n]选择任何项目。

然后在演示中,我们将foo,bar,baz替换为三个片段引用,以使演示更接近您想要的内容。

最后一步是使用预处理指令__${...}__来确保片段选择器被th:replace:视为字符串

<th:block th:replace = "__${#strings.listSplit(customFragments, ',')[1]}__">

最后的想法

如果重新排列模板和片段可以让你简化,避免这种复杂性,那么我会提倡这样做,而不是这样做。但我不知道更大的背景,所以这可能不是一个选择。

最新更新