dpwu16132 2013-08-06 05:44
浏览 82
已采纳

Twig无法识别传递给模板的变量

I'm trying to render several things on the page at once (latest articles, newest events, ...). I'm using Symfony2 with Doctrine2.

This is what my controller code looks like:

  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
    ));
  }

The dump of the events reveals that it's a StdArray with one item in it (the event from my database). However, if I try to visit the page I get the following Symfony2 error:

Variable "events" does not exist in MyAppBundle:Page:index.html.twig at line 47

The relevant Twig template part is as follows (it's the exact same as the articles):

{% 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 &raquo;</a></p>
  <hr class="dotted">
{% endfor %}

It seems to me that variables aren't being passed into the view properly, since I can't even display a hardcoded string (i.e. 'fruit', 'banana').

Has anyone got a clue as to why this is happening?

  • 写回答

1条回答 默认 最新

  • douzi7890 2013-08-06 05:58
    关注

    'events', $events should be changed to 'events' => $events

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?