Dead Simple PHP Template Rendering

Ever want to break a small PHP app into an MVC-ish convention without the need of tacking on a massive templating engine? Are you opposed to remembering new syntax just for a template? Why not just use some ob_* code to roll your own simple templating engine?

This will allow you to create PHP includes which will render as templates so you can organize your app into an MVC-ish layout.

<?php
  class MissingTemplateException extends Exception {}
  
  function render_template($template_file, $vars = array())
  {
    if(file_exists($template_file))
    {
      ob_start();
        extract($vars);
        include($template_file);
      return ob_get_clean();
    }else
      throw new MissingTemplateException("Template: {$template_file} could not be found!");
  }
  
  function my_action()
  {
    $name = 'Rob';
    $company = 'Globalcom';
    return render_template('template.phtml', array('name' => $name, 'company' => $company));
  }
  
  echo my_action(); // => "Hello Rob! You work at Globalcom, don't you?"
?>

Usage

Hello <?= $name ?>! You work at <?= $company ?>, don't you?

View Gist

Simple.