Template inheritance in plain PHP

I’m looking for a way to build plain PHP templates with inheritance. I’ve used phpti before, but it’s not working properly within Slim. It works fine with a simple example, but when I have multiple blocks, it fails miserably. I really don’t want to use Twig.

All I did, was include phpti in index, and use this route:

$app->get('/', function (Request $request, Response $response) {
    $response = $this->view->render($response, "/pages/home.php");
    return $response;
});


// home.php
<?php include APP_VIEWS . "/template.php"; ?>

<?php startblock('head'); ?>
<link rel="stylesheet" href="/assets/css/layout.css">
<?php endblock('head'); ?>

<?php startblock('content'); ?>
<h3>Some Content</h3>
<?php endblock('content'); ?>

// template.php
<html>
<head>
    <?php emptyblock('head'); ?>
</head>
<body>
<h1>Here's the header</h1>
<?php emptyblock('content'); ?>
</body>
</html>`Preformatted text`

Solution: Just add <?php flushblocks(); ?> to the end of your home.php.

The problem is that Slim starts output buffering before PHPTI, and PHPTI fails if ob_get_level() > 1; This is common issue for all projects using PHPTI, and definitely there should be some less ugly workaround for it, but I know only this one.

UPD: I found a better place where to put this flushblocks(). Inherit your View class from Slim\View, redefine it’s render() method, copy all code from original and add flushblocks() before return:

    ...
    extract($data);
    ob_start();
    require $templatePathname;
    flushblocks();           //  <---  add it here;
    return ob_get_clean();
 }