Easy PHP templates

Tuesday, February 21st, 2012

Found this browsing the Sitepoint forums. A great and simple method for building templates in PHP:

class Template extends ArrayObject {
  protected $file = null;
  public function setFile( $file ) {
    $this->file = $file;
  public function __toString() {
    return ob_get_clean();
// In your controller...
$template = new Template();
$template->setFile( 'myTemplate.phtml' );
// Fictional model method to get rowset from a query
$template['rows'] = $db->getRows( $query );
$template['title'] = 'Hello World';
echo $template;

Thanks, Michael Morris.

Original post by Michael Morris

Leave a Reply