Created
March 29, 2010 17:40
-
-
Save robhurring/348149 to your computer and use it in GitHub Desktop.
PHP Template Rendering
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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?" | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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