2013-01-23 73 views
10

我必须使用Twig变量作为另一个Twig变量的属性。使用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 %} 

感谢。

+3

你试过的[属性功能(http://twig.sensiolabs.org/doc/functions/attribute.html)? – mbosecke

+0

@mbosecke:这对我有用,只需将它写为答案,以便我可以接受它,谢谢:) –

回答

10
{% for object in objects %} 
     {% for column in columns %} 
      {{ attribute(object, column) }} {# equivalent to php $object[$column] #} 
     {% endfor %} 
    {% endfor %} 

使用特威格Attribute Function(嫩枝> 1.2)