I use tTwo ways you can do this:
Simplest way:
<?php include('header.php'); ?>
content
<?php include('footer.php'); ?>
This works great with page controllers.
The included files have all the variables from the page that includes them.
Second way, using templates:
Wrapper.tpl.php
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $menu; ?>
<?php echo $content; ?>
<div id="footer">my footer</div>
</body>
</html>
Menu.tpl.php
<ul class="menu">
<li class="header"><?php echo $title; ?></li>
<li><a href="#">item</li>
<li><a href="#">item</li>
<li><a href="#">item</li>
</ul>
Content.tpl.php
<div class="contentWide">
lore ipsum...
</div>
And in PHP, the usage would be something like this:
$tpl = new Template('Wrapper.tpl.php');
$tpl->title = 'my super duper page';
$tpl->menu = new Template('Menu.tpl.php');
$tpl->menu->title = 'My Menu Title';
$tpl->content = new Template('Content.tpl.php');
echo $tpl;
Where the Template class would be something like:
class Template {
private $file = null;
private $data = null;
function __constructor($pFile) {
$this->file = $pFile;
}
public function __set($key, $value) {
$this->data[$key] = $value;
}
public function __get($key) {
return $this->data[$key];
}
public function __toString() {
extract ($this->data);
ob_start ();
include ($this->file);
$contents = ob_get_contents ();
ob_end_clean ();
return $contents;
}
}
I like the second method because you can control what data gets passed where, you can cache components easier, and your HTML blocks are complete (so you can use them anywhere without having to worry about what HTML makes up the page your adding them to.) It’s also easier to use from front controllers, or chain controllers.
But, the first method is allot simpler and faster.