我试图一次在页面上呈现几个东西(最新的文章,最新的事件,.)。我将Symfony2与Doctrine2结合使用。
这就是我的控制器代码的样子:
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$articles = $this->getDoctrine()->getRepository('MyAppBundle:Article')->findOrderedByDate(3);
$events = $this->getDoctrine()->getRepository('MyAppBundle:Event')->findOrderedByDate(2);
\Doctrine\Common\Util\Debug::dump($events);
return $this->render('MyAppBundle:Page:index.html.twig', array(
'articles' => $articles,
'events', $events
));
}
事件转储显示它是一个StdArray,其中包含一个项(来自我的数据库的事件)。但是,如果我试图访问该页面,则会得到以下Symfony2错误:
Variable "events" does not exist in MyAppBundle:Page:index.html.twig at line 47
相关的Twig模板部分如下(与文章完全相同):
{% for event in events %}
<h2>{{ event.name }}</h2>
<p class="small">{{ event.eventdate|date('Y-m-d H:i:s') }}</p>
<p>{{ event.intro }} <a href="#">Lees meer »</a></p>
<hr class="dotted">
{% endfor %}
在我看来,变量没有被正确地传递到视图中,因为我甚至不能显示硬编码的字符串(即“水果”、“香蕉”)。
有人知道为什么会发生这种事吗?
发布于 2013-08-06 05:58:40
'events', $events
应改为'events' => $events
https://stackoverflow.com/questions/18072574
复制相似问题