symfony树枝渲染控制器参数数组 [英] symfony twig render controller argument array
问题描述
我想在诸如以下的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) { }
与我通过树枝中的render/controller函数发送给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.
非常感谢你, Stef.
Thank you so much, Stef.
推荐答案
似乎是括号错误.在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屋!