A third, and slightly more advanced way to work with templates is by extending them; i.e. insert a template into another template. Here is an example:
<head>
<title>
Here is the title:
<!-- title -->
Default title [will be replaced with $content]
<!-- /title -->
</title>
</head>
<body>
<div id=content>
<!-- content -->
Default content [will be replaced with $content]
<!-- /content -->
</div>
<div class=sidebar>
<!-- sidebar -->
Sidebar has no content [will be replaced with $sidebar]
<!-- /sidebar -->
</div>
<!-- footer -->
Should remain untouched...
<!-- /footer -->
</body>
Content template:
<!-- title -->
My title
<!-- /title -->
<!-- content -->
My body
<!-- /content -->
Sidebar template:
<!-- sidebar -->
Sidebar contents
<!-- /sidebar -->
And the code:
$tpl = new Stamp($base);
$tpl->extendWith($content)->extendWith($sidebar);
copyright © 2011 Gabor de Mooij | Stamp PHP Template Library