Trying to figure out the best way to get some variables into the master twig template. Will be needed to do individually for every route, or sometimes for a group to simplify things.
This is the group and route I have right now, and it seems to work. The globalTemplateVars middleware gets all the data for the header and footer of site , and the other closure middleware allows to get some variables into the header and footer template.
$app->group('/getpassword', function (RouteCollectorProxy $group) {
$group->map(['GET', 'POST'],'/', \App\Application\Controllers\PasswordResetController::class .':index');
// some more routes related to password reset here.
})
->add($globalTemplateVars)
->add(function (Request $request, RequestHandler $handler) use ($app) {
$setupVars = array(
'forgotpasswordpage' => true,
'SECTION' => 'ADMIN'
);
$this->set('setupVars', $setupVars);
$response = $handler->handle($request);
return $response;
});
This is the middleware that populates the master twig template for the header and footer
$globalTemplateVars = function (Request $request, RequestHandler $handler) use ($app) {
/**
* @var \Slim\Views\Twig
*/
$view = $this->get('view');
$templatesData = $this->get('MasterTemplateDataLoader')->getData();
$view->getEnvironment()->addGlobal('global', $templatesData);
$this->set(Twig::class, $view);
$response = $handler->handle($request);
return $response;
};
Here are my issues that need to figure out…
-
is there a better way to inject some route conditional variables into the globalTemplateVars middleware before it runs? When I had globalTemplateVars on the group but defined the vars on or in the route, it wasn’t early enough to get the vars in there.
-
is there a way to not populate the master twig view template until it’s used similar to ViewComposers in Laravel?
-
I am hoping to not have to call the globalTemplateVars middleware or the closure middleware on every route. Something more programatic would be nice.