使用 Twig 中的数组键访问数组值
有谁知道如何(如果可能的话!)在 Twig 中做到这一点?我在文档中看不到任何关于它是否可能的内容。
PHP 数组的结构如下:
$data['data']['a']['title'] = 'Title 1';
$data['data']['a']['title'] = 'Title 2';
$data['data']['b']['title'] = 'Title 3';
Twig 模板代码如下:
{% for letter in 'a'..'z' %}
{{ letter }}
<ul>
{% for key, item1 in data %}
{% for item2 in item1 %}
<li>{{ item2[key].title }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endfor %}
编辑: 经过进一步调查,出现了该属性(参见 http://twig.sensiolabs.org/doc/functions/attribute.html) 函数应该可以完成这项工作,但我不确定如何在这些情况下使用它。
非常感谢
Does anyone know how (of if it's even possible!) to do this in Twig? I can't see anything in the documentation as to whether it's possible.
The PHP array is structured as below:
$data['data']['a']['title'] = 'Title 1';
$data['data']['a']['title'] = 'Title 2';
$data['data']['b']['title'] = 'Title 3';
Twig template code below:
{% for letter in 'a'..'z' %}
{{ letter }}
<ul>
{% for key, item1 in data %}
{% for item2 in item1 %}
<li>{{ item2[key].title }}</li>
{% endfor %}
{% endfor %}
</ul>
{% endfor %}
Edit: After further investigation, it appears the attribute (see http://twig.sensiolabs.org/doc/functions/attribute.html) function should do the job but I am unsure as to how to use it in these circumstances.
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你刚刚尝试过这个吗:
Have you just tried this: