这可能相对容易做到,但我对Twig不熟悉而且感到沮丧。
我正在调整来自这个答案的代码:https://dev59.com/82Ag5IYBdhLWcg3wBXKs#24058447
这个数组是通过PHP使用这种格式创建的:
$link[] = array(
'link' => 'http://example.org',
'title' => 'Link Title',
'display' => 'Text to display',
);
然后通过 twig,我为其添加 HTML,再将其合并:
<ul class="conr">
<li><span>{{ lang_common['Topic searches'] }}
{% set info = [] %}
{% for status in status_info %}
{% set info = info|merge(['<a href="{{ status[\'link\'] }}" title="{{ status[\'title\'] }}">{{ status[\'display\'] }}</a>']) %}
{% endfor %}
{{ [info]|join(' | ') }}
</ul>
但我得到的是:
错误码[8]:将数组转换为字符串,位于F:\localhost\www\twig\include\lib\Twig\Extension\Core.php 的第832行
当我删除这一行时问题解决了,但没有显示内容:
{{ [info]|join(' | ') }}
有什么方法可以正确地使这个东西崩溃?
** 更新 **
使用Twig的dump函数返回了空值。似乎它甚至没有将其加载到数组中。我如何将信息加载到新数组中。
join
将支持两个参数:glue
和and
("and
: 用于最后一对输入项的分隔符[...]and
参数已在Twig 1.37中添加。")。 - Sandra