如何检索数组元素的值(字符串)并使用它来检索实体的属性值?

时间:2014-09-23 11:42:30

标签: symfony twig

如何检索数组元素的值(字符串)并检索实体的属性值?我试过这个:

{% for item in items %} //item is an entity
  {% for column in columns %} //column is just an array with name of columns
    {% set columna = column.value %}
    {{ item.columna }}
  {% endfor %}
{% endfor %}

2 个答案:

答案 0 :(得分:1)

如果要动态访问对象的属性,可以使用attribute function

{% for item in items %}
  {% for column in columns %}
    {{ attribute(item , column.value) }}
  {% endfor %}
{% endfor %}

答案 1 :(得分:0)

试试这个:

{% for item in items %} //item is an entity
  {% for column in columns %} //column is just an array with name of columns
    {% set columna = column.value %}
    {{ attribute(item, columna) }}
  {% endfor %}
{% endfor %}

我建议您阅读documentation