Output array in Twig
Asked Answered
C

5

22

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?

Cytolysin answered 13/10, 2014 at 12:33 Comment(0)
Z
7

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?)

Zestful answered 13/10, 2014 at 14:26 Comment(0)
P
31

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.

Polymyxin answered 13/10, 2014 at 14:25 Comment(0)
B
24

You can use join to output an array as a concatenated string. It behaves like implode() in php.

Example:

{{ [1, 2, 3]|join }}
{# returns 123 #}

{{ [1, 2, 3]|join('|') }}
{# outputs 1|2|3 #}

{{ [1, 2, 3]|join(', ', ' and ') }}
{# outputs 1, 2 and 3 #}

See the twig join documentation.

Baltimore answered 1/8, 2016 at 11:28 Comment(1)
Here the new link fot the doc : twig.symfony.com/doc/3.x/filters/join.htmlExpansile
Z
7

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?)

Zestful answered 13/10, 2014 at 14:26 Comment(0)
G
0

For anybody who want to output an associative array easily :

(here, the array is user.profile.category)

        <table>
            <tr>
                {% for key,value in user.profile.category[0] %}
                    <td>{{key|e }}</td>
                {% endfor %}
            </tr>

            {% for cat in user.profile.category %}
                <tr>
                    {% for cell in cat %}
                        <td>{{ cell|e }}</td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </table>
Gause answered 18/1, 2018 at 15:49 Comment(0)
A
0

In Opencart 4 you need to add $this->addExtension(new \Twig\Extension\DebugExtension());

in public function __construct(LoaderInterface $loader, $options = []) system\storage\vendor\twig\twig\src\Environment.php

and enable set debug' => true, // Initialize Twig environment

in public function render(string $filename, array $data = [], string $code = ''): string { system\library\template\twig.php

Agonistic answered 19/3, 2023 at 4:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.