0

I need to pass an array to a Twig template. My code for this is:

<?php


require_once '../vendor/autoload.php';


require_once '../config/generated-conf/config.php';


Twig_Autoloader::register();


$loader = new Twig_Loader_Filesystem('vistas');
$twig = new Twig_Environment($loader);
$twig->addExtension(new Twig_Extension_Debug());

// Get planes list with Propel ORM
$planes = PlaneQuery::create()->find();
var_dump($planes->toArray());


echo $twig->render('admin-planes.html.twig', $planes->toArray());
?>

When I execute var_dump($planes) it returns array's content, but when I do {{ dump(planes) }} on Twig, it returns nothing...

I'm using Propel ORM for getting data.

Any idea? Perhaps I missing something that I can't figure out.

Neniel
  • 163
  • 16

2 Answers2

1

You need to pass the variables in an associative array. The array indexes are the names of the variables as accessed by twig.

echo $twig->render('admin-planes.html.twig', [ 'planes' => $planes->toArray()]);
user2182349
  • 9,569
  • 3
  • 29
  • 41
  • Thanks a lot, I forgot that. Just a thing... It wasn't necessary the call to the toArray() method (at least for what I needed). – Neniel Jun 09 '16 at 02:52
1

Do this

    $data['planes'] = $planes->toArray();
    echo $twig->render('admin-planes.html.twig', $data);     
zivklen
  • 79
  • 2
  • 10