如何在twig中 call 一个保存在字符串变量中的function


attribute

New in version 1.2: The attribute function was added in Twig 1.2.
attribute can be used to access a "dynamic" attribute of a variable:
1
2
3
{{ attribute(object, method) }}
{{ attribute(object, method, arguments) }}
{{ attribute(array, item) }}
比如:

 
{% for column in columns %}
  {% if  column=='activity' %}
   {% for activity in company.activity %}
   {% endfor %}
  {% else if column='contacts' %}
   {% for contact in company.contacts %}
   {% endfor %}
  {% else %}
   {{ attribute(company, column) }}
  {% endif %}
 {% endfor %}

评论

热门博文