# Getting information into master template

**URL:** https://discourse.slimframework.com/t/getting-information-into-master-template/3579
**Category:** Questions
**Created:** [October 25, 2019, 1:26am UTC](https://discourse.slimframework.com/t/getting-information-into-master-template/3579 "2019-10-25T01:26:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![dunkoh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dunkoh/32/995_2.png) [@dunkoh](https://discourse.slimframework.com/u/dunkoh)
#### Post date: [October 25, 2019, 1:26am UTC](https://discourse.slimframework.com/t/getting-information-into-master-template/3579/1 "2019-10-25T01:26:06Z")

</div>

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…

1. 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.

2. is there a way to not populate the master twig view template until it’s used similar to ViewComposers in Laravel?

3. I am hoping to not have to call the globalTemplateVars middleware or the closure middleware on every route. Something more programatic would be nice.

---

<div class="post-metadata">

### Author: ![dunkoh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dunkoh/32/995_2.png) [@dunkoh](https://discourse.slimframework.com/u/dunkoh)
#### Post date: [October 25, 2019, 2:04am UTC](https://discourse.slimframework.com/t/getting-information-into-master-template/3579/2 "2019-10-25T02:04:40Z")

</div>

This all based on how our legacy pages are constructed which we’re migrating away from.

```
$someVar = true;

require_once header (use $someVar in here to do something like load DataTables library, or to not enforce login on a page, etc)

some code in the page

require_once footer
```

---

<div class="post-metadata">

### Author: ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)
#### Post date: [October 25, 2019, 6:58am UTC](https://discourse.slimframework.com/t/getting-information-into-master-template/3579/3 "2019-10-25T06:58:41Z")

</div>

With PHP-DI and constructor (dependency) injection you could let the container inject the object with data into the controller/action classes you need.

---

<div class="post-metadata">

### Author: ![pedropereira](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/pedropereira/32/982_2.png) [@pedropereira](https://discourse.slimframework.com/u/pedropereira)
#### Post date: [October 25, 2019, 8:53am UTC](https://discourse.slimframework.com/t/getting-information-into-master-template/3579/4 "2019-10-25T08:53:39Z")

</div>

I’m with odan.  
You could make it a dependency of the request handler constructor. Inject only in the Handler classes you need. Any container implementation could help you with that.  
But your use of middlewares for doing that is also good I think. The only difference is with a container with autowiring you only have to make that definition once and then add it as a dependency where you need to. 🙂

---

<div class="post-metadata">

### Author: ![dunkoh](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/dunkoh/32/995_2.png) [@dunkoh](https://discourse.slimframework.com/u/dunkoh)
#### Post date: [October 25, 2019, 3:22pm UTC](https://discourse.slimframework.com/t/getting-information-into-master-template/3579/5 "2019-10-25T15:22:34Z")

</div>

Thanks for the replies. Based on thought from @odan it gave me the idea to inject the class that loads all the master template stuff and have it return an instance of twig view so that can act on it in a chain in the controller function, essentially mimicking a require\_once in a way…but still being able to dictate it for each route

```
return $this->headerAndFooter->getData(array(
            'forgotpasswordpage' => true,
            'SECTION' => 'ADMIN'
        ))->render($response, 'passwordreset/index.twig', $templateData);
```
