dongqi8863 2016-01-09 13:00
浏览 128
已采纳

如何在Twig上添加多维数组中的元素?

I have this array called theme in a Twig template:

array:4 [▼
  "foo" => "bar"
  "headerimage" => array:6 [▶]
  "templatefields" => array:1 [▶]
  "assets" => array:3 [▼
    "css" => array:1 [▶]
    "js" => array:1 [▶]
    "libs" => array:2 [▼
      0 => "jquery"
      1 => "bootstrap"
    ]
  ]
]

I wanna add more elements in theme.assets.libs . I try with:

{% set theme.assets.libs = theme.assets.libs|merge(['otherlibrary', 'anotherlibrary']) %}

But I have the next error.

Unexpected token "punctuation" of value "." ("end of statement block" expected) in "index.twig" at line 7.

  • 写回答

1条回答 默认 最新

  • doujiyuan0211 2016-01-09 13:04
    关注

    Twig doesn't allow doing this directly. You can instead, however, do this by continuing the pattern of array merges you're already doing:

    {% set theme = theme|merge({assets: theme.assets|merge({ libs: theme.assets.libs|merge(['otherlibrary', 'anotherlibrary']) }) }) %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部