gpt4 book ai didi

twig - 将 include 的内容保存到变量中

转载 作者:行者123 更新时间:2023-12-02 09:23:11 24 4
gpt4 key购买 nike

我有一个 Twig 模板,它使用 batch() 制作一些像这样的列:

{% set rows = collection|batch(3) %}

如此处记录:http://twig.sensiolabs.org/doc/filters/batch.html此函数采用第二个参数来定义用于“缺失”元素的字符串。

我正在寻找类似的东西:

{% set html = include 'path/to/file.html.twig' %} <-- parse error
{% set rows = collection|batch(3, html) %}

有没有办法做到这一点或我可以/应该做的其他事情?

最佳答案

您可以使用以下代码段分配一段文本:

main.twig

{% set foo %}
{% include 'foo.twig' %}
{% endset %}

{% set items = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] %}

<table>
{% for row in items|batch(3, foo) %}
<tr>
{% for column in row %}
<td>{{ column }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>

foo.twig

<div>
<h1>Foo</h1>
</div>

twigfiddle

关于twig - 将 include 的内容保存到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40152442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com