RenderLayout Method for a Custom MVC Framework
Let's now create the renderLayout
method.
This method will take the layout file and substitute
the values of the variables $title
and $content
(it will be passed
as a parameter of the method and will represent
the result of the renderView
method):
<?php
private function renderLayout(Page $page, $content) {
$layoutPath = $_SERVER['DOCUMENT_ROOT'] . "/project/layouts/{$page->layout}.php";
if (file_exists($layoutPath)) {
ob_start();
$title = $page->title;
include $layoutPath; // here the variables $title and $content will be available
return ob_get_clean();
}
}
?>