excluded excluded - 4 months ago 11
Twig Question

Output array in Twig

I trying to output array from database to screen.
In my entity:

/**
* @ORM\Column(type="array", nullable=true)
*/
private $category;


In my twig template:

{% for category in user.profile.category %}
{{ category }}
{% endfor %}


Error:
Array to string convertion in ...


Where is my mistake ?

Answer

TWIG doesn't know how you want to display your table.

By the way, you should consider naming your variable $categories instead of $category, as you table contains several categories.

Then try this:

{% for category in user.profile.categories %}
   {{ category }}
{% endfor %}

If my answer doesn't help, please give us the structure of your array (is there any keys or sub-arrays in you table or is it just a list?)

Comments