Reputation: 327
I have a real strange problem with twig, I iterate over an array and when I try to 'print' value with {{value}}
I get the exception "Array to string conversion" and when I try with {{value.first()}}
I get the error "Impossible to invoke a method ("first") on a string variable ("false")"
Can someone help me out ?
<select name="select">
{% for key,value in array %}
{% if (key != 'id') and (key != 'type') %}
<option value={{key}}>{{ key }}: {{ value }}</option>
{% endif %}
{% endfor %}
</select>
Upvotes: 0
Views: 116
Reputation: 2167
I think the best way to handle this would be to pass the data already flatten to Twig, so it would only need to loop through the data always in the same way.
If that's not possible and you need to use Twig to handle this, you can make use of iterable
. This is not pretty, but here we go:
<select name="select">
{% for key,value in array %}
{% if (key != 'id') and (key != 'type') %}
{% if value is iterable %}
{% for item in value %}
<option value={{item}}>{{ key }}: {{ item }}</option>
{% endfor %}
{% else %}
<option value={{key}}>{{ key }}: {{ value }}</option>
{% endif %}
{% endif %}
{% endfor %}
</select>
Upvotes: 1