Search code examples
phplatte

Latte: Didn't find overgiven parameter


I try to use the latte template engine. I register the Engine and try to render a template with parameters:

$latte = new Latte\Engine;

$events = new Event((new Config())->connectDB());
$result = $events->getAll();

$latte->render('templates/events.latte', $result);

This is the part from the template file:

{layout 'base.latte'}

{block title}Veranstaltungsübersicht{/block}

{block content}
  {foreach $result as $row}
....

When I try this, I get this eror:

Notice: Undefined variable: result in ...../vendor/latte/latte/src/Latte/Engine.php(179) : eval()'d code on line 60

But when I make a var_dump($result) directly before $latte I get this:

array(1) { [0]=> array(8) { ["address"]=> string(38) "Street" ["date"]=> string(10) "2015-05-15" ["desc"]=> string(20) "sadfasdfasdfasdfasdf" ["event_participant"]=> int(25) ["id"]=> int(1) ["name"]=> string(12) "Church" ["reservation_date"]=> string(10) "2015-05-10" ["church_id"]=> int(1) } }

So I don get it. Why can latte not find the $result variable?


Solution

  • In the current code, you pass no "name" for that $result array to the template, only the array itself. Thus, your rendering engine does not know the name of that variable, but only the content.

    To make this work, change the render call to:

    $latte->render('templates/events.latte', ['result' => $result]);