symfony twig 渲染控制器参数数组数组、控制器、参数、symfony

2023-09-06 23:44:36 作者:你们女人太悲催。

我想在 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 }) }}

希望有帮助