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

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

问题描述

我想将数组作为参数发送到 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.

在默认控制器中,我把这个:

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 树枝渲染控制器参数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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