Search code examples
phparraystwigvar-dump

Twig gets an empty array but var_dum($myarray) says it's not empty


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.


Solution

  • 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()]);