Mark Gregory Yap
Mark Gregory Yap

Reputation: 1

Why does my array become empty when I pass it to Twig

Controller:

$app->get('/', function() use($app){
    $feeds[0] = "test";
    $feeds['name'] = "name";
    $app->render('home.twig', $feeds);
})->name('home');

Twig:

<h2>
{% if feeds %}
    {{ feeds[0] }}
    {{ feeds.name }}
{% else %}
    <h2 style="color: white">TEST</h2>
{% endif %}
</h2>

It displays TEST instead of the array that I used. When I try to var_dump it, the array contains the data yet it shows as empty in Twig.

Upvotes: 0

Views: 74

Answers (1)

Louay Alakkad
Louay Alakkad

Reputation: 7408

Use this instead:

$app->render('home.twig', array('feed' => $feeds));

Upvotes: 5

Related Questions