Reputation: 321
I have a problem with show the value from variables. For example, i have function for choose columns which i want to render. I sent the 2 variables for TWIG schema to show. First is variable which stores all data from some table (e.g. user) and second for stored columns which we choosed from this tables to show. Now in the twig and want to do two loops, for show next entity and show columns from entity. The better explanation is below example but this is not working.
{% for user in users %}
{% for column in columnts%}
<li>{{ user.column}}</li>
{% endfor %}
{% endfor %}
Somebody know how can i solve this ?
Upvotes: 3
Views: 2532
Reputation: 13891
What about using the attribute twig helper, which is commonly used to access any dynamic attribute of a given variable.
{% for user in users %}
{% for column in columns %}
<li>{{ attribute(user, column) }}</li>
{% endfor %}
{% endfor %}
Also, "attribute(user, column) is defined
" should help you check for the existence of your dynamic attribute/method.
Upvotes: 3
Reputation: 5877
Try something like that:
{% for user in users %}
{% for column in columns %}
{% if attribute(user, column) is defined %}
<li>{{ attribute(user, column) }}</li>
// or attribute(user, 'get' ~ column|capitalize) if you have getters for your properties
{% endif %}
{% endfor %}
{% endfor %}
Upvotes: 3