Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Twig variable as a property name of an other Twig variable

Tags:

I must use a Twig variable as a property for another Twig variable.

In a for loop, I get the properties of a specific entity and want to use those properties to get the property-content for an entity variable in an other for loop.

Some code to make this clear:

{% 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 %} 

Thanks.

like image 894
Aykut Çevik Avatar asked Jan 23 '13 18:01

Aykut Çevik


1 Answers

The attribute function is what you are looking for.

Edit:

  {{ attribute(object, method) }}   {{ attribute(object, method, arguments) }}   {{ attribute(array, item) }} 
like image 51
mbosecke Avatar answered Sep 21 '22 14:09

mbosecke