symfony twig 渲染控制器参数数组 [英] symfony twig render controller argument array

查看:35
本文介绍了symfony twig 渲染控制器参数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 twig 命令中发送一个数组作为参数,例如:

I would like to send an array as an argument in a twig command like:

{{ render(controller("AppBundle:Default:Test"), { 'myarray': array }) }}

但我无法找出好方法.让我们用基本的 AppBundle 来解释以下简单示例.在我的项目中,渲染器会要求来自另一个 Bundle 的渲染器.我确信这个过程是相同的,无论它是否是同一个 Bundle.

But I'm not able to figure out the good way. Let's explain the following simple example with the basic AppBundle. In my project, the render will ask for a render from another Bundle. I'm sure the process is the same, whenever it's the same Bundle or not.

在默认的Controller中,我放了这个:

In the default Controller, I put this:

 /**
 * @Route("/test", name="test")
 */
public function testAction()
{
    return $this->render('AppBundle:Default:Test.html.twig', array (
        'tests' => array("Test 1", "Test 2", "Test 3", "Test 4")
    ));
}

/**
 * @Route("/test2", name="test2")
 */
public function test2Action($tests = array())
{
    var_dump($tests);

    return $this->render('AppBundle:Default:Test2.html.twig', array(
        'tests' => $tests
    ));
}

我添加了一个 var_dump 来跟踪数组,它没有转发到 test2Action 函数.

I added a var_dump to track the array, and it is not forwarded to the test2Action function.

在 Test.html.twig 中,我有这段代码:

In the Test.html.twig, I have this code:

{{ render(controller("AppBundle:Default:Test2"), { 'tests': tests }) }}

在 Test2.html.twig 中,我有这段代码:

In the Test2.html.twig, I have this code:

{% for test in tests %}
    {{ test }}</br>
{% endfor %}

最后,我在导航器中有这个:

Finally, I have this in the navigator:

array(0) { }

没有关于我通过 twig 中的渲染/控制器函数发送给 test2Action 函数的数组.

Nothing about the array I sent to the test2Action function through the render/controller function in twig.

我使用的是 Symphony 3.0.3,但即使在 Symphony 2.8 中,我也找不到任何相关信息.

I'm using Symphony 3.0.3, but even in Symphony 2.8, I cannot find any relevant information.

也许我没有使用最好的方法来做到这一点.

Maybe I'm not using the best way to do this.

拜托,你能帮帮我吗?我真的需要将一个数组从一个包发送到另一个包,以便两者相互独立.

Please, could you help me. I really need to send an array from a bundle to another, in order to have both independent from the other.

非常感谢,史蒂夫.

推荐答案

似乎是括号错误.在 Test.html.twig 中,试试这个:

Seems a bracket mistake. In the Test.html.twig, try this:

{{ render(controller("AppBundle:Default:Test2", { 'tests': tests }) ) }}

代替:

{{ render(controller("AppBundle:Default:Test2"), { 'tests': tests }) }}

希望有帮助

这篇关于symfony twig 渲染控制器参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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