Trying to write a template engine with support for nested templates. The idea is that in the constructor of each controller, an object of the View class is created, which in itself already contains the basic site layou with the variable $content . The $content variable should load the View that calls the controller (if it exists at all).
The problem is that I still can not understand how to substitute $content into an already loaded layout $content
The class itself looks like this at the moment.
<?php namespace App\Controllers; class View { private $variables = array(); /** * Устанавливает имена и значения перменных передаваемых в шаблон * * @param string $name Имя переменной в шаблоне * @param mixed $value Значение переменной в шаблоне * * @return $this */ public function assign($name, $value) { $this->variables[$name] = $value; return $this; } /** * Возвращает указанный шаблон в виде строки. * * @param string $template Путь к шаблону * * @return bool|string Возращает строку с шаблоном, либо false, * если шаблон не найден. */ public function render($template) { extract($this->variables); ob_start(); if (file_exists($template)) { require_once $template; return ob_get_clean(); } ob_end_clean(); return false; } /** * Отображает указанный шаблон. * * @param string $template Путь к шаблону * * @return bool Возвращает true если шаблон удалось загрузить * false - в случае неудачи. */ public function display($template) { extract($this->variables); ob_start(); if (file_exists($template)) { require_once $template; echo $content = ob_get_clean(); return true; } ob_end_clean(); return false; } }