Created
October 2, 2013 15:45
-
-
Save rohmann/6795815 to your computer and use it in GitHub Desktop.
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 View { | |
public static function __callStatic($name,$parameters) { | |
try { | |
//Get template name from method call | |
$fname = TEMPLATES_DIR . implode('/', explode("_",$name) ).'.php'; | |
//Ensure this template exists | |
if(file_exists($fname)) { | |
//Set default parameters | |
if(!isset($parameters[0])) | |
$parameters[0] = array(); | |
if(!isset($parameters[1])) | |
$parameters[1] = true; | |
if(!isset($parameters[2])) | |
$parameters[2] = true; | |
//Get parameters | |
list($data,$echo,$extract) = $parameters; | |
//By default the context will be extracted. This can be overriden if the template needs direct access | |
if($extract) | |
extract($data); | |
ob_start(); | |
include($fname); | |
$buffer = ob_get_clean(); | |
//Function will always buffer and return, but can echoing can be disabled. | |
if($echo) | |
echo $buffer; | |
return $buffer; | |
} | |
else { | |
throw new \Exception( "View Template not found: $fname", 1); | |
} | |
} | |
catch (\Exception $e) { | |
trigger_error($e->getMessage() , E_USER_WARNING); | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
This loads templates/admin/dashboard/widget.php and creates a variable called $hello containing "world" ready for use in the template.