Sub-templates in PHP-View

The README for PHP-View says:

Sub-templates

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

… without further details. Browsing around, I got the impression that the mechanism is something like:

<footer><?php echo $this->fetch('inc/footer.phtml'); ?></footer>

… but that only gets me to:

Maximum function nesting level of ‘256’ reached, aborting!

The stack trace definitively shows an infinite loop of:

  • C:[…]\vendor\slim\php-view\src\PhpRenderer.php(230): include(‘C:\[…]…’)
  • C:[…]\vendor\slim\php-view\src\PhpRenderer.php(210): Slim\Views\PhpRenderer->protectedIncludeScope(‘C:\[…]…’, Array)
  • C:[…]\html\layout\general.phtml(14): Slim\Views\PhpRenderer->fetch(‘inc/footer.pht…’)

Not sure what’s being included because paths are cropped.

Rest of code goes like:

$this->config->renderer->setLayout('layout/general.phtml');
return $this->config->renderer->render(
    $response,
    'example.phtml',
    [
        'title' => 'Example Page',
    ]
);

I already know many workarounds but I’d like to figure out how to use the built-in feature as intended.

I’ve been completely unable to make it work so I’ve written my own method:

<?php

namespace Alvaro\Views;

class PhpRenderer extends \Slim\Views\PhpRenderer
{
    public function block(string $template, array $data = []): string
    {
        $layout = $this->layout;
        $this->layout = null;
        $output = parent::fetch($template, $data);
        $this->layout = $layout;
        return $output;
    }
}

Related: Issue #49: Sub-templates and layouts