Просмотр поста

.
Delphinum

Ну как вариант можно так:

Сам шаблонизатор:

class Template{
  protected $file;
  protected $data;

  public function __construct($file, array $data = []){
    $this->file = $file;
    $this->data = $data;
  }

  public function __toString(){
    extract($this->data);
    ob_start();
    include($this->file);
    return ob_get_clean();
  }
}


Страница:
<div>
  <?= $data ?>
</div>


Layout:
<!DOCTYPE html>
<html>
  <head>
    <title><?= $title ?></title>
    <meta charset="utf-8" />
  </head>
  <body>
    <?= $content ?>
  </body>
</html>


Используется так:
$page = new Template('page.html', ['data' => 'Hello world']);
$layout = new Template('layout.html', ['title' => 'Test title', 'content' => $page]);
echo $layout;


На выходе получается так:
<!DOCTYPE html>
<html>
  <head>
    <title>Test title</title>
    <meta charset="utf-8" />
  </head>
  <body>
    <div>
      Hello world
    </div>
  </body>
</html>