thomastuts
thomastuts

Reputation: 3539

Twig doesn't recognize variable passed into template

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?

Upvotes: 1

Views: 1152

Answers (1)

Tuan nguyen
Tuan nguyen

Reputation: 570

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

Upvotes: 3

Related Questions