21

我必须使用 Twig 变量作为另一个 Twig 变量的属性。

在 for 循环中,我获取特定实体的属性,并希望使用这些属性来获取另一个 for 循环中实体变量的属性内容。

一些代码可以清楚地说明这一点:

{% for entity in entities  %}

{{entity.foo}}, {{entity.bar}}<br />

{% for property in specialdynamicproperties %}
{{entity.property}} <!-- property has the content foobar for example, I want to use it as the property accessor for entity -->
{% endfor %}

{% endfor %}

谢谢。

4

2 回答 2

34

属性函数就是你要找的。

编辑:

  {{ attribute(object, method) }}
  {{ attribute(object, method, arguments) }}
  {{ attribute(array, item) }}
于 2013-01-23T19:49:22.143 回答
17
    {% for object in objects %}
        {% for column in columns %}
            {{ attribute(object, column) }} {# equivalent to php $object[$column] #}
        {% endfor %}
    {% endfor %}

使用 Twig属性函数(Twig > 1.2)

于 2013-10-21T14:25:23.517 回答