动态树枝变量名称 [英] Dynamic twig variable names

查看:35
本文介绍了动态树枝变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Given an array of variables sent to a twig template, such as:

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

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

I want to capture the variables for easy access in twig. However:

{% for key, value in form.vars.attr %}
    {% set key = value %}
{% endfor %}

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

remaps the key variable in the for loop.

树枝对象:

{% for key, value in form.vars.attr %}
    {% set {{key}} = value %}
{% endfor %}

据我所知,堆栈似乎从未设置过地址.有谁知道,请指出如何完成这个变量赋值?

And stack as I am aware never seems to address set. Would anyone who knows, please indicate how to accomplish this variable assignment?

推荐答案

我知道这个语法有效

{% render "..." with {(key): value} %}

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

Did you try the following syntax? As of March, Friday 22nd this syntax didn't work so you need to use a work around.

{% set (key) = value %}

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

An alternative to that would be to include a template and pass and form.vars.attr.

{% include "YourAwesomeBundle:Controller:template.html.twig" with form.vars.attr %}

您还可以使用 合并功能.

You can also merge form.vars.attr with another array using the merge function.

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

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

Within the included template you will be able to use the variable em and group.

这篇关于动态树枝变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆