Documentations

Twig data transfert

In your controller, when calling the twig rendering engine (with the $this->twig->render function) it is possible to send data to the views which can be used as variables.

Here is an example that we will be able to explain in detail.

$itemManager = new ItemManager();
$items = $itemManager->selectAll();

return $this->twig->render('Item/index.html.twig', [
  'items' => $items,
]);

In this piece of code (from the index method of itemController.php), after calling the table manager and calling the selectAll method, the results are stored in the $items variable.

Next, the twig->render will create a view from the file (in the view folder) Item/index.html.twig.

In this file, the item variable will be available to be displayed there (as long as it is a string or a number)

It is possible to send as many variables as you want, like each element of the array passed as a parameter, the key is the name of the variable available in twig.

return $this->twig->render('Item/index.html.twig', [
  'items' => $items,
  'theFirstItem' => $items[0],
  'nameOfSomething' = "the name of something",
]);

To know the content of a variable, when the site is not in production (by default) it is possible to display an equivalent of var_dump() in the view with the dump() function.

{{ dump(theFirstItem) }}