Slim 4 and Unable to get data in an array to show up in Twig

To pass a variable to Twig, the array must contain such a key.

Example:

$query = $this->connection->createQueryBuilder();

$users = $query
    ->select('id', 'username')
    ->from('users')
    ->executeQuery()
    ->fetchAllAssociative() ?: [];

$viewData = [
    'users' => $users,
];

return $this->twig->render($response, 'loop.html.twig', $viewData);
2 Likes