Parcourir la source

index html twig rendering

Twig
Ruben De Baets il y a 2 ans
Parent
révision
8e2f69ed08
2 fichiers modifiés avec 19 ajouts et 8 suppressions
  1. 7
    6
      public/index.php
  2. 12
    2
      templates/index.html.twig

+ 7
- 6
public/index.php Voir le fichier

@@ -24,8 +24,6 @@ $twig = new \Twig\Environment($loader, []);

//$db = new DB();
ini_set('display_errors', 'Off');


function addNavbar($response)
{

@@ -37,14 +35,12 @@ function addNavbar($response)
}
return;
}

function addFooter($response)
{
$response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>');
$response->getBody()->write("</body></html>");
return;
}

function init()
{
$art1 = ["slug" => "artikel-1", "title" => "Dit is titel 1", "content" => "Lorem ipsum 1"];
@@ -54,15 +50,20 @@ function init()
$_SESSION['blogs'] = [$art1, $art2, $art3, $art4];
}


$app->get('/', function (Request $request, Response $response, array $args) {
if (!isset($_SESSION['blogs'])) {
init();
}

$vars = [
"key" => "value",
'key2' => ["val1", "val2", "val3"],
"key3" => ["x1" => "y1", "x2" => "y2"]
];

global $twig;
$template = $twig->load('index.html.twig');
$a = $template->render();
$a = $template->render($vars);
$response->getBody()->write($a);
return $response;
});

+ 12
- 2
templates/index.html.twig Voir le fichier

@@ -1,6 +1,16 @@
<html>
<body>
<h1>Blog posts</h1>
<h1>{{ key }}</h1>

<hr/>
<ul>
{% for key in key2 %}
<li>{{ key }}</li>
{% endfor %}

{{ key3.x1}}
</ul>

</body>
</html>
</html>

Chargement…
Annuler
Enregistrer