【发布时间】:2013-08-06 05:44:41
【问题描述】:
我正在尝试一次在页面上呈现几件事情(最新文章、最新事件……)。我正在使用 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 %}
在我看来,变量没有正确传递到视图中,因为我什至无法显示硬编码字符串(即“fruit”、“banana”)。
有人知道为什么会这样吗?
【问题讨论】:
标签: php symfony doctrine-orm twig