Twig 仅显示第一个数组级别
Twig only showing first array level
我第一次在论坛上提问。我通常使用 blade 进行模板制作,但独立的 Twig 更易于安装。所以我有这个问题,希望你能帮助我。
$breadcrums['titles'] = ['Home','Toys','Ball'];
$breadcrums['links'] = ['/','/toys','/toys/ball'];
{% for item in breadcrums %}
<a href={{ item.links }}> {{ item.titles }} </a>
{% endfor %}
我正在尝试实现类似的功能,但代码没有打印任何内容。另一方面,如果这样做:
{% for value in breadcrums.titles %}
{{ value }}
{% endfor %}
工作正常,但不是我想要做的。谢谢
我建议您将 $breadcrumbs
更改为
$breadcrumbs = [
'Home' => '/',
'Toys' => '/toys',
'Ball' => '/toys/ball'
];
这使您的数组更具可读性,您可以轻松地看到标题及其相关 link(而不是有两个 "unrelated" 数组)。
在您的树枝模板中,您可以使用:
{% for title,link in breadcrumbs %}
<a href="{{ link }}">{{ title }}</a>
{% endfor %}
更多属性的解决方案:
如果您想向面包屑添加更多属性,您可以使用类似的东西:
$breadcrumbs = [
[
'title' => 'Home',
'url' => '/',
'atitle' => 'Link to the home page'
],
[
'title' => 'Toys',
'url' => '/toys',
'atitle' => 'Every toy you can imagine'
],
[
'title' => 'Ball',
'url' => '/toys/ball',
'atitle' => 'Ballroom blitz'
]
];
然后在你的 twig 文件中使用:
{% for breadcrumb in breadcrumbs %}
<a href="{{ breadcrumb.url }}" title="{{ breadcrumb.atitle }}">{{ breadcrumb.title }}</a>
{% endfor %}
将输出:
<a href="/" title="Link to the home page">Home</a>
<a href="/toys" title="Every toy you can imagine">Toys</a>
<a href="/toys/ball" title="Ballroom blitz">Ball</a>
我第一次在论坛上提问。我通常使用 blade 进行模板制作,但独立的 Twig 更易于安装。所以我有这个问题,希望你能帮助我。
$breadcrums['titles'] = ['Home','Toys','Ball'];
$breadcrums['links'] = ['/','/toys','/toys/ball'];
{% for item in breadcrums %}
<a href={{ item.links }}> {{ item.titles }} </a>
{% endfor %}
我正在尝试实现类似的功能,但代码没有打印任何内容。另一方面,如果这样做:
{% for value in breadcrums.titles %}
{{ value }}
{% endfor %}
工作正常,但不是我想要做的。谢谢
我建议您将 $breadcrumbs
更改为
$breadcrumbs = [
'Home' => '/',
'Toys' => '/toys',
'Ball' => '/toys/ball'
];
这使您的数组更具可读性,您可以轻松地看到标题及其相关 link(而不是有两个 "unrelated" 数组)。
在您的树枝模板中,您可以使用:
{% for title,link in breadcrumbs %}
<a href="{{ link }}">{{ title }}</a>
{% endfor %}
更多属性的解决方案:
如果您想向面包屑添加更多属性,您可以使用类似的东西:
$breadcrumbs = [
[
'title' => 'Home',
'url' => '/',
'atitle' => 'Link to the home page'
],
[
'title' => 'Toys',
'url' => '/toys',
'atitle' => 'Every toy you can imagine'
],
[
'title' => 'Ball',
'url' => '/toys/ball',
'atitle' => 'Ballroom blitz'
]
];
然后在你的 twig 文件中使用:
{% for breadcrumb in breadcrumbs %}
<a href="{{ breadcrumb.url }}" title="{{ breadcrumb.atitle }}">{{ breadcrumb.title }}</a>
{% endfor %}
将输出:
<a href="/" title="Link to the home page">Home</a>
<a href="/toys" title="Every toy you can imagine">Toys</a>
<a href="/toys/ball" title="Ballroom blitz">Ball</a>