Templates in MVC in PHP
The views we studied in the previous lesson actually represent the content of the page. Besides the content, the page usually also has a header, sidebars, and a footer. These parts are typically the same on all pages of the site.
In our framework, each page of the site is represented by the same HTML template file, to which the page content from the view is connected to a specified place for each site page.
The template file is located at the following
path: /project/layouts/default.php.
According to the framework rules, this file
has access to the variable $content. At the
point where this variable is output,
the page content will be inserted.
By default, this file contains the following simple code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?= $content ?>
</body>
</html>
Place the following site layout in the template file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>page title</title>
</head>
<body>
<header>
site header
</header>
<div class="container">
<aside class="sidebar left">
left sidebar
</aside>
<main>
<?= $content ?>
</main>
<aside class="sidebar right">
right sidebar
</aside>
</div>
<footer>
site footer
</footer>
</body>
</html>
Go to any action of any controller. See what has changed.