Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig accessing array key where is space (PHP allow space on the array key)

Tags:

php

twig

symfony

PHP allow in array to be a space in the key like this:

(dump from symfony2)

array:9 [▼
"Guest" => 1
"Vip Client" => 2
"Super admin" => 3
]

So how can I access the data by key in Twig? Normaly for the Guest it will be {% array_name.Guest %} this will not work {% array_name['Vip Client'] %}

like image 958
fr3sh Avatar asked Nov 12 '15 09:11

fr3sh


2 Answers

You can use attribute function

{{ attribute(array_name, 'Vip Client') }}

As suggested here: http://twig.sensiolabs.org/doc/templates.html#variables

like image 87
Martin Patera Avatar answered Oct 14 '22 17:10

Martin Patera


You can access like this: {{ array_name['Guest'] }} if you want to print. If you want to use it if condition you can write

{% if array_name['Vip Client'] == 1 %} {{'Vip Client'}} {% endif %}
like image 25
Isabek Tashiev Avatar answered Oct 14 '22 18:10

Isabek Tashiev