2

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
  • Subject of your post should look like a "subject" rather than set of odd keywords! You should make an effort to come with a meaningful subject. Commented Mar 17, 2016 at 9:33

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'm stupid. It is 2 weeks I'm trying to solve this. Thank you Matteo. I would prefer that Twig return an error, instead of returning "null". Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.