Created
January 26, 2015 14:31
-
-
Save ethaizone/4a68217c10268f27cce9 to your computer and use it in GitHub Desktop.
Example view render as helper
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 | |
if (! function_exists('view')) | |
{ | |
/** | |
* Render view as html with layout system | |
* @author Nimit Suwannagate <ethaizone@hotmail.com> | |
*/ | |
function view($viewName, $data = array(), $layout = 'default') | |
{ | |
if (! is_array($data)) | |
{ | |
throw new Exception("View data must be array."); | |
} | |
extract($data); | |
//generate view html | |
$viewFile = "views/{$viewName}.php"; | |
ob_start(); | |
include($viewFile); | |
$yield = ob_get_contents(); | |
ob_end_clean(); | |
// generate layout | |
// html from view will show to layout as $yield variable | |
$layoutFile = "views/layouts/{$layout}.php"; | |
ob_start(); | |
include($layoutFile); | |
$html = ob_get_contents(); | |
ob_end_clean(); | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment