Skip to content

Instantly share code, notes, and snippets.

@robhurring
Created March 29, 2010 17:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robhurring/348149 to your computer and use it in GitHub Desktop.
Save robhurring/348149 to your computer and use it in GitHub Desktop.
PHP Template Rendering
<?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?"
?>
Hello <?= $name ?>! You work at <?= $company ?>, don't you?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment