I have this php file
<?php
// Load our autoloader
require __DIR__.'/vendor/autoload.php';
// Specify our Twig templates location
$loader = new Twig_Loader_Filesystem(__DIR__.'/views');
// Instantiate our Twig
$twig = new Twig_Environment($loader);
echo json_encode ($products);
echo $twig->render('index.twig', ['$products' => $products] );
But i don't even know how to pass my products.json file in my index.twig, but im sure that it's reading my file because output is this one:
and my JSON file is this :
{
"products":[
{
"name" : "Notebook",
"description" : "Core i7",
"value" : "800.00",
"date_register": "2017-06-22"
},
{
"name" : "Mouse",
"description" : "Razer",
"value" : "125.00",
"date_register": "2017-10-25"
},
{
"name" : "Keyboard",
"description" : "Mechanical Keyboard",
"value" : "250.00",
"date_register": "2017-06-23"
}
] }
$products
is a JSON string when encoded. Leave it decoded as you did in $twig->render('index.twig', ['$products' => $products] );
and you can access it like a regular object.
In the twig template, you can then use :
{% for product in $products.products %}
<tr>
<td>{{ product.name }}</td>
<td>{{ product.description }}</td>
<td>{{ product.value }}</td>
<td>{{ product.date_register }}</td>
</tr>
{% endfor %}