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