Accessing array values using array key from Twig

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

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 }}     
    {% for key, item1 in data %} {% for item2 in item1 %}
  • {{ item2[key].title }}
  • {% endfor %} {% endfor %}
{% 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

回答1:

Have you just tried this:

attribute(item2, key).title


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!