Views
Unic framework support multiple view engine like php, twig etc.
Let’s set the default view directory path:
$app->set('views', __DIR__ . '/views'));
Let’s set the view engine and configuration options:
$app->set('view_engine', 'twig', [
'cache' => false
]);
Now let’s create a view file index.twig
inside the views directory:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
Let’s render a view:
$app->get('/', function($req, $res) {
$res->render('index.twig');
});
We can pass data to the views:
$app->get('/', function($req, $res) {
$res->locals->a = 10;
$res->render('index.twig', [
'title' => 'Unic',
]);
});
The $res->locals
variables are by default accessible from views.