Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TWIG, dynamic associative array key

Tags:

twig

I need to create a array with a dynamic key.

Example:

{% set key = 'a' %}
{% set value = 'b' %}
{% set array = {key:value} %}
like image 789
Павел Бражко Avatar asked Oct 06 '16 10:10

Павел Бражко


1 Answers

I find a workaround for this problem: if you surround the key with parenthesis the key of array take the value of the variable instead of the name. So try simple this:

{% set array = { (key): value} %}

Here a working solutions

like image 113
Matteo Avatar answered Oct 22 '22 06:10

Matteo