Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.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
Php Symfony细枝路径函数参数,键由字符串定义_Php_Symfony_Twig - Fatal编程技术网

Php Symfony细枝路径函数参数,键由字符串定义

Php Symfony细枝路径函数参数,键由字符串定义,php,symfony,twig,Php,Symfony,Twig,在Twig with Symfony中,要在模板中生成路径,它希望使用 {{ path('mypathname', {parameterName: parameterValue}) }} 在我的项目中,我有一个继承的模板,一个块循环遍历一块数据,并吐出一个URL列表。我想做的是,根据我的新模板,允许我{%set%}第一个参数的键名 所以基本上是这样的 template.html.twig <ul> {% for thing in things %} <li>&l

在Twig with Symfony中,要在模板中生成路径,它希望使用

{{ path('mypathname', {parameterName: parameterValue}) }}
在我的项目中,我有一个继承的模板,一个块循环遍历一块数据,并吐出一个URL列表。我想做的是,根据我的新模板,允许我
{%set%}
第一个参数的键名

所以基本上是这样的

template.html.twig

<ul>
{% for thing in things %}
    <li><a href="{{ path(path_name, {path_param: thing.value}) }}">{{ thing.name }}</a></li>
{% endfor %}
</ul>
{% extends 'template.html.twig' %}
{% set path_name = "thingDetail" %}
{% set path_param = "id" %}
然后查看如下输出

<ul>
    <li><a href="/things/detail?id=20">The 20th Thing</a></li>
</ul>
这听起来可能很可笑,但在整个应用程序中,命名和路径样式并不总是与进入块循环的不同类型的内容相匹配,因此需要进行某种类型的覆盖,而我现在最不想做的就是为每种类型的块覆盖块,当最后需要做的事情是URL生成时


谢谢大家!

如果将哈希键括在括号中,Twig会将其视为表达式:

{%- set field = 'foo' -%}
{%- set arr = { (field): 'bar' } -%}
arr
设置为

{ foo: 'bar' }

(在中提到了这一点,但很容易看出它是如何被忽略的。)

如果将哈希键括在括号中,Twig会将其视为一个表达式:

{%- set field = 'foo' -%}
{%- set arr = { (field): 'bar' } -%}
arr
设置为

{ foo: 'bar' }

(这在中提到,但很容易看出它是如何被忽略的。)

您是一位绅士和学者,感谢您指出:)您是一位绅士和学者,感谢您指出:)