Created
June 5, 2013 15:47
-
-
Save atbradley/5714931 to your computer and use it in GitHub Desktop.
Generate HTML from a PHP template (http://stackoverflow.com/questions/1309800/php-eval-that-evaluates-html-php/1309805#1309805)
This file contains hidden or 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
define('TEMPLATE_DIR', 'path/to/my/templates'); | |
/** | |
* Processes a PHP template. | |
* | |
* Looks in OCRA_TEMPLATE_DIR for {$template}.tpl, and includes it, | |
* probably generating an HTML page. | |
* | |
* @author Adam Bradley <[email protected]> | |
* | |
* @param string $template The template file to use | |
* @param array $data Data To be passed to the template. | |
* @param boolean $return If true, return a string, otherwise print the result to the browser. | |
* | |
* @return boolean|string The template results (if $return=true) or the return value of ob_end_flush() | |
* | |
* @todo Should throw an exception if the template file doesn't exist. | |
*/ | |
function do_template($template, Array $data, $return=false) { | |
//TODO: Should throw an exception if the template doesn't exist. | |
extract($data); | |
ob_start(); | |
include(TEMPLATE_DIR ."/$template.tpl"); | |
if ( $return ) return ob_get_flush(); | |
return ob_end_flush(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment