使用Twig中的数组键访问数组值 [英] Accessing array values using array key from Twig

查看:155
本文介绍了使用Twig中的数组键访问数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道如何在Twig中做到这一点吗?我看不到文档中是否有任何可能.

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.

PHP数组的结构如下:

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';

下面的树枝模板代码:

{% 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 )函数可以完成这项工作,但是我不确定在这种情况下如何使用它.

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.

非常感谢

推荐答案

您刚刚尝试过吗:

attribute(item2, key).title

这篇关于使用Twig中的数组键访问数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆