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) {
  require("templates/" . $filename . ".php");
  $contents = ob_get_contents(); 
  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.