【问题标题】:Twig doesn't recognize variable passed into templateTwig 无法识别传入模板的变量
【发布时间】: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 &raquo;</a></p>
  <hr class="dotted">
{% endfor %}

在我看来,变量没有正确传递到视图中,因为我什至无法显示硬编码字符串(即“fruit”、“banana”)。

有人知道为什么会这样吗?

【问题讨论】:

    标签: php symfony doctrine-orm twig


    【解决方案1】:

    'events', $events 应改为'events' =&gt; $events

    【讨论】:

    • 好吧,现在我觉得自己真的很蠢。那解决了它。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-04
    • 2012-11-21
    • 2019-02-27
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多