2014-09-23 47 views
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 %} 
    {{ item.columna }} 
    {% endfor %} 
{% endfor %} 
+1

而究竟是什麼不起作用?你看到一些錯誤嗎? – 2014-09-23 11:48:34

回答

1

如果要動態地訪問一個對象的屬性,你可以使用attribute function

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

@感謝!!,它的工作原理,我忘了說'column.value'返回一個錯誤。只是'{{屬性(項目,欄)}}'爲我工作。 – ziiweb 2014-09-23 13:13:41

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