Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
964 views
in Technique[技术] by (71.8m points)

symfony twig render controller argument array

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...