Embed template within another template

Hi all,

First time posting here and I’m pretty new to it all. I’m trying to keep a navbar at the top of the screen that’s defined in a template, but I don’t know how - or if it’s even possible - to embed the navbar template into another template.

Example use-case: I have a navbar I’ve created and want to put it on all my pages: Home, About, Contact, etc. How do I include that navbar template into the Home template, About template, and Contact template without copy-pasting the raw code?

I guess my top-level question is: how can I make Slim’s use of templates modular?

I’m not sure what template engine you’re using, but if you’re using Twig, you can do this:

{% include 'include/header.twig' %}

Hope this helps …

1 Like

Maybe I don’t understand well enough. Can this be done without twig with raw phtml files?

I have no idea, I’ve never used PHTML files … but a quick google search turns up things like this:

Maybe/Hopefully this helps …

one way is to make your controller extend an abstract controller class, in abstract controller make a generic render method like so:

protected function render($response, $template, $args)
{
$this->view->render($response, ‘include/header.php’, $args);
$this->view->render($response, $template, $args);
$this->view->render($response, ‘include/footer.php’, $args);
}

then call it at the end of your controller

$this->render($response, ‘page/myTemplate.php’, $args);
return $response;