I try to avoid PHP but a university course mandated its use some time ago and I decided to do the best of the situation. One of the things I did was to implement this simple function for template rendering:
<?
function render_template($filename, $variables) {
extract($variables);
ob_start();
require("templates/" . $filename . ".php");
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
?>
It can be used like this:
<?
function layout($title, $body) {
return render_template("layout.html", array("title" => $title, "body" => $body));
}
?>
This allows for very simple and nice composition of templates. Additionally, passing around all the template data explicitly like this makes it easier to stay sane.