The key value is replaced with the "key" when using merge () in the branch

I am trying to add key value pairs to an array with their current values ​​for all those attributes that do not start with '_'. For some reason, the merge replaces the key value (for example, slug) with the string key.

For example, when slug is the only attribute with a key not starting with '_',

key = slug value = something 

he behaves as follows:

 {% for key,value in app.request.attributes.all %} {% if '_' != key | slice(0, 1) %} {{ dump(key) }} // string(4) "slug" {% set params = params | merge({ key : value}) %} {{ dump(key) }} // string(4) "slug" {% endif %} {% endfor %} {{ dump(params) }} // array(1) { ["key"]=> string(9) "something" } 

I added that dumps are returning next to them.

Last dump returns

 array(1) { ["key"]=> string(9) "something" } 

while i expect

 array(1) { ["slug"]=> string(9) "something" } 

I would say that this is a similar problem for Twig forgets the array keys , but the conclusion on this is the mongodb problem, and I am not using it. I am working with attributes from a request.

For some reason, a merge ({key: value}) behaves like a merge ({'key': value}).

+7
source share
1 answer

You need to wrap your variable with a parenthesis in order to use it as a key.

 {% set params = params | merge({ (key) : value}) %} 
+22
source

All Articles