dongqi8863 2016-01-09 21: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 21: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']) }) }) %}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 如何利用闲置机械硬盘变现
    • ¥15 信号处理中的凸优化问题
    • ¥15 arm虚拟机无法和物理机互通
    • ¥15 如何在此代码上增加一个统计学生生源的功能?(语言-c语言)
    • ¥15 Android导航条遮盖异常
    • ¥15 计算机网络技术基础问题
    • ¥15 设置mac系统只能访问指定网站
    • ¥15 西门子博途 s7 1200控制三台步进电机
    • ¥15 基于非参数的方向距离函数求污染物影子价格(有偿)
    • ¥15 vue+element 生成table