Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

twig using variables as hash key

Tags:

php

twig

symfony

Is it possible to use the path function dynamically? With variables as parameters?

{{ path(nav.url, { nav.param_name : nav.param_value }) }}

Because if I try to use that code I am getting:

A hash key must be followed by a colon (:). 
Unexpected token "punctuation" of value "." 
("punctuation" expected with value ":") 

I know by passing 'hardcoded' the param_name will work... like this:

{{ path('nav.url', { 'id' : nav.param_value }) }}

However I need to pass the 3 value dynamically

like image 952
Michael Barquero Avatar asked Feb 01 '13 01:02

Michael Barquero


1 Answers

The Twig book says you have to enclose expressions in parentheses to use them as keys, so maybe this will work:

{{ path(nav.url, { (nav.param_name) : nav.param_value }) }}

http://twig.sensiolabs.org/doc/templates.html#literals

like image 74
matt Avatar answered Nov 13 '22 21:11

matt