簡體   English   中英

使用Twig變量作為其他Twig變量的屬性名稱

[英]Use Twig variable as a property name of an other Twig variable

我必須使用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 %}

謝謝。

屬性函數是您正在尋找的。

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

使用Twig 屬性函數 (Twig> 1.2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM