Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I send an associative array into an included Twig template?

I'm trying to send an associative array into a Twig template I am including:

{% include 'my-template.twig' with {
    items: [
        [
            'property' => 'value',
            'other' => 'value'
        ],
        [
            'property' => 'value',
            'other' => 'value'
        ]
    ]
} %}

I get this error:

PHP Fatal error: Uncaught exception 'Twig_Error_Syntax' with message 'An array element must be followed by a comma. Unexpected token "operator" of value "=" ("punctuation" expected with value ",")

It would seem it doesn't like =>. Is it my syntax? Is there a way to accomplish this?

like image 790
Michael Lynch Avatar asked Dec 23 '15 18:12

Michael Lynch


2 Answers

change associative array start and end [] to {} and keys => to :

[
        {
            'property':'value',
            'other':'value'
        },
        {
            'property':'value',
            'other':'value'
        }
]

http://twig.sensiolabs.org/doc/templates.html#literals

like image 89
Max P. Avatar answered Sep 30 '22 04:09

Max P.


{% include 'my-template.twig' with {
    items: {
        {
            'property': 'value',
            'other': 'value'
        },
        {
            'property': 'value',
            'other': 'value'
        }
    }
} %}
like image 22
lsklyut Avatar answered Sep 30 '22 02:09

lsklyut