[英]Twig doesn't recognize variable passed into template
我试图一次在页面上呈现几件事(最新文章,最新事件等等)。 我正在使用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 %}
在我看来,变量没有正确地传递到视图中,因为我什至无法显示硬编码的字符串(即“水果”,“香蕉”)。
有谁知道为什么会这样吗?
'events', $events
应该更改为'events' => $events
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.