Twig/Symfony2 - 在数组合并中使用变量 [英] Twig / Symfony2 - using a variable inside array merge

查看:33
本文介绍了Twig/Symfony2 - 在数组合并中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{% set var_name1 = "hello" %}
{% set var_name2 = "there" %}
{% array1|merge({var_name1: var_name2}) %}

我希望上面的代码将它添加到 array1:

I was hoping the code above would add this to array1:

hello:there

...但它补充说:

var_name1:there

我尝试将 {{ }} 包裹在 var_name1 周围.是否可以将记录添加到数组并使用变量作为键?

I've tried wrapping {{ }} around var_name1. Is it possible to add a record to an array and use a variable for the key?

推荐答案

将键名括在方括号中:

{% array1|merge({(var_name1): var_name2}) %}

这篇关于Twig/Symfony2 - 在数组合并中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆