动态树枝变量名称

Lig*_*art 3 symfony twig

给定一组发送到树枝模板的变量,例如:

$form   = $this->createForm( new ServiceItemType()
                           , $entity
                           , array( 'attr'=>
                                     array(
                                          'em'    => $this->EM()
                                         ,'group' => true
                           ) ) );
Run Code Online (Sandbox Code Playgroud)

我想捕获变量以便在树枝中轻松访问.然而:

{% for key, value in form.vars.attr %}
    {% set key = value %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

重新映射for循环中的键变量.

树枝对象:

{% for key, value in form.vars.attr %}
    {% set {{key}} = value %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

我知道堆栈似乎永远不会解决集合问题.有谁知道,请说明如何完成这个变量赋值?

Tho*_*ire 5

我知道这种语法有效

{% render "..." with {(key): value} %}
Run Code Online (Sandbox Code Playgroud)

您是否尝试过以下语法?截至3月22日星期五,这种语法不起作用,因此您需要使用一种解决方法.

{% set (key) = value %}
Run Code Online (Sandbox Code Playgroud)

另一种方法是包含模板并传递和form.vars.attr.

{% include "YourAwesomeBundle:Controller:template.html.twig" with form.vars.attr %}
Run Code Online (Sandbox Code Playgroud)

您还可以form.vars.attr使用合并功能与另一个阵列合并.

{% set vars = {} %}
{% set vars = vars|merge(form.vars.attr) %}
{% include "YourAwesomeBundle:Controller:template.html.twig" with vars %}
Run Code Online (Sandbox Code Playgroud)

在包含的模板中,您将能够使用变量emgroup.