Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 使用变量作为twig中的散列键(作为path()或| trans中的参数)_Symfony_Twig - Fatal编程技术网

Symfony 使用变量作为twig中的散列键(作为path()或| trans中的参数)

Symfony 使用变量作为twig中的散列键(作为path()或| trans中的参数),symfony,twig,Symfony,Twig,是否可以动态使用trans filter或path函数?以变量作为参数? 例1: 因为:我不知道“object”是使用id还是slug进行路由 例2: 因为:我不知道它是“%user%”还是“%article%”或其他什么 为什么这样做很好: {{ path('object_edit', { 'id' : parameter_value }) }} 但这并不是: {{ set parameter_type = 'id' }} {{ path('object_edit', { parameter

是否可以动态使用trans filter或path函数?以变量作为参数? 例1:

因为:我不知道“object”是使用id还是slug进行路由

例2:

因为:我不知道它是“%user%”还是“%article%”或其他什么

为什么这样做很好:

{{ path('object_edit', { 'id' : parameter_value }) }}
但这并不是:

{{ set parameter_type = 'id' }}
{{ path('object_edit', { parameter_type : parameter_value }) }}
印刷品:

array(1) { ["foobar"]=> string(11) "hello world" } 

用括号括起来可以解决问题。

用括号(而不是大括号)括起哈希键

{{ set parameter_type = 'id' }}
{{ path('object_edit', { parameter_type : parameter_value }) }}
{% set key = 'foobar' %}
{% set hash = { (key) : 'hello world' } %}
{% debug hash %}
array(1) { ["foobar"]=> string(11) "hello world" } 
{{ path('object_edit', { (parameter_type) : parameter_value }) }}