如何在 Twig 上的多维数组中添加元素?
How add elements in a multdimensional array on Twig?
我在 Twig 模板中有这个名为 theme 的数组:
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"
]
]
]
我想在 theme.assets.libs 中添加更多元素。我尝试:
{% set theme.assets.libs = theme.assets.libs|merge(['otherlibrary', 'anotherlibrary']) %}
但是我有下一个错误。
Unexpected token "punctuation" of value "." ("end of statement block"
expected) in "index.twig" at line 7.
Twig 不允许直接这样做。但是,您可以通过继续您已经在执行的数组合并模式来执行此操作:
{% set theme = theme|merge({assets: theme.assets|merge({ libs: theme.assets.libs|merge(['otherlibrary', 'anotherlibrary']) }) }) %}
我在 Twig 模板中有这个名为 theme 的数组:
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"
]
]
]
我想在 theme.assets.libs 中添加更多元素。我尝试:
{% set theme.assets.libs = theme.assets.libs|merge(['otherlibrary', 'anotherlibrary']) %}
但是我有下一个错误。
Unexpected token "punctuation" of value "." ("end of statement block" expected) in "index.twig" at line 7.
Twig 不允许直接这样做。但是,您可以通过继续您已经在执行的数组合并模式来执行此操作:
{% set theme = theme|merge({assets: theme.assets|merge({ libs: theme.assets.libs|merge(['otherlibrary', 'anotherlibrary']) }) }) %}