使用变量作为哈希键的twig

时间:2022-10-24 14:15:51

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

是否可以动态使用路径功能?用变量作为参数?

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

{{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:

我知道通过'硬编码',param_name将起作用......就像这样:

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

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

However I need to pass the 3 value dynamically

但是我需要动态传递3值

1 个解决方案

#1


26  

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

Twig书说你必须在括号中包含表达式才能将它们用作键,所以这可能会起作用:

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

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

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

#1


26  

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

Twig书说你必须在括号中包含表达式才能将它们用作键,所以这可能会起作用:

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

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

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