我可以将关联数组发送到包含的Twig模板吗?

时间:2015-12-23 18:57:30

标签: php include twig associative-array

我试图将关联数组发送到我包含的Twig模板中:

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

我收到此错误:

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 ",")

看起来它不像=>。这是我的语法吗?有没有办法实现这个目标?

2 个答案:

答案 0 :(得分:5)

将关联数组的开始和结束[]更改为{},将=>更改为:

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

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

答案 1 :(得分:2)

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