symfony twig render controller argument array

2020-02-15 02:08发布

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

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

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
    ));
}

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

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

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

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) { }

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

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.

Thank you so much, Stef.

1条回答
姐就是有狂的资本
2楼-- · 2020-02-15 02:49

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

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

instead of:

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

Hope this help

查看更多
登录 后发表回答