Output array in Twig

nowiko picture nowiko · Oct 13, 2014 · Viewed 75.4k times · Source

I trying to output an array from the database to the 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 conversion in ...

Where is my mistake?

Answer

NHG picture NHG · Oct 13, 2014

So, as error shows you are trying convert array (in category variable) to string. You can preview array by dump() (doc.). In your case:

{% for category in user.profile.category %}
    {{ dump(category) }}
{% endfor %}

Please notice that dump() should be use only for debugging.