Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sub-templates and layouts #49

gdsmith opened this issue Jul 8, 2019 · 3 comments


Copy link

commented Jul 8, 2019

From the documentation:

Inside your templates you may use $this to refer to the PhpRenderer object to render sub-templates.

Including sub-templates (with fetch) when you are using layouts is problematic as sub-template content also includes the surrounding layout. Using a straight php include(template.file) is an effective workaround.


This comment has been minimized.

Copy link

commented Aug 6, 2019


method fetch

if ($this->layout !== null) {
    $data['content'] = $output;
    $this->protectedIncludeScope($this->layout, $data);
    $output = ob_get_clean(); 

maybe the problem is here $this->layout == null
if $this->layout == null - in this case object return only sub-template


This comment has been minimized.

Copy link

commented Aug 9, 2019

README explains you can render sub-templates but doesn't say how:

  • If you are meant to use \Slim\Views\PhpRenderer::fetch(), as in <footer><?php echo $this->fetch('footer.phtml'); ?></footer>, then you get Maximum function nesting level of '256' reached, aborting! breaking an infinite loop.
  • If that isn't the way, I guess docs need a tweak.

This comment has been minimized.

Copy link

commented Aug 19, 2019

Here's a potential patch I created which gives a workaround for template-less partials:

--- PhpRenderer.php.orig        Mon Aug 19 15:37:38 2019
+++ PhpRenderer.php     Mon Aug 19 15:39:37 2019
@@ -204,7 +204,7 @@
             $this->protectedIncludeScope($this->templatePath . $template, $data);
             $output = ob_get_clean();

-            if ($this->layout !== null) {
+            if ($data['layout'] !== false && $data['layout'] !== '' && $this->layout !== null) {
                 $data['content'] = $output;
                 $this->protectedIncludeScope($this->layout, $data);


    echo $this->fetch('partials/test.phtml', [
        'layout'  => false,

Another possibility would be to create a separate method which wouldn't include the layout, regardless (e.g.$this->fetchPartial('partials/test.phtml')).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
4 participants
You can’t perform that action at this time.