PHP: render_template

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", array("title" => $title, "body" => $body));
}
?>

This allows for simple composition of templates and separation of views and models. Additionally, passing around all the template data explicitly like this makes it easier to stay sane.

I’ve published a runnableā„¢ project that you can play around with.