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

**URL:** https://discourse.slimframework.com/t/slim-4-and-unable-to-get-data-in-an-array-to-show-up-in-twig/5792
**Category:** Questions
**Created:** [January 12, 2024, 4:25pm UTC](https://discourse.slimframework.com/t/slim-4-and-unable-to-get-data-in-an-array-to-show-up-in-twig/5792 "2024-01-12T16:25:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![MarkJC](https://avatars.discourse-cdn.com/v4/letter/m/ebca7d/32.png) [@MarkJC](https://discourse.slimframework.com/u/MarkJC)
#### Post date: [January 12, 2024, 4:25pm UTC](https://discourse.slimframework.com/t/slim-4-and-unable-to-get-data-in-an-array-to-show-up-in-twig/5792/1 "2024-01-12T16:25:24Z")

</div>

Hi I am wondering if I am going nuts.  
I am able to get a list of users from my MYSQL database but seem unable to show them in the Twig view. Am I missing something?

My route: $app-\>get(‘/loop’, ‘App\Controller\LoopController:loop’);

My Controller:

```php
<?php

namespace App\Controller;

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Views\Twig;
use Doctrine\DBAL\Connection;

class LoopController
{
   private Twig $twig;
   private Messages $flash;
   private Connection $connection;

   public function __construct(Twig $twig, Messages $flash, Connection $connection)
   {
       $this->twig = $twig;
       $this->flash = $flash;
       $this->connection = $connection;
   }

   public function loop(Request $request, Response $response, array $args): Response
   {
       $query = $this->connection->createQueryBuilder();

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

       $users = $rows;
       //var_dump($users); <<<<this brings back the array of users
       return $this->twig->render($response, 'loop.html.twig', $users);
       // <<<<<Array $users should be available to the view when the route is entered.
   }
}

```

The loop.html.twig view:

```auto
       {% include 'header.twig' %}
       <p>Welcome to my homepage. Thanks for visiting!</p>
       <h1>Users</h1>
       <ul>
    {% for user in users %}
        <li>{{ user.username|e }}</li>
    {% endfor %}
     </ul>
      {% include 'footer.twig' %}

```

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [January 12, 2024, 8:30pm UTC](https://discourse.slimframework.com/t/slim-4-and-unable-to-get-data-in-an-array-to-show-up-in-twig/5792/2 "2024-01-12T20:30:42Z")

</div>

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

Example:

```php
$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);

```
