【问题标题】:Can I send an associative array into an included Twig template?我可以将关联数组发送到包含的 Twig 模板中吗?
【发布时间】:2016-03-30 06:19:57
【问题描述】:

我正在尝试将关联数组发送到我包含的 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 ",")

它似乎不喜欢=>。是我的语法吗?有没有办法做到这一点?

【问题讨论】:

    标签: php include twig associative-array


    【解决方案1】:
    {% include 'my-template.twig' with {
        items: {
            {
                'property': 'value',
                'other': 'value'
            },
            {
                'property': 'value',
                'other': 'value'
            }
        }
    } %}
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-06
        • 2016-12-01
        • 2012-12-25
        • 2012-02-09
        相关资源
        最近更新 更多