Skip to content

Instantly share code, notes, and snippets.

@ethaizone
Created January 26, 2015 14:31
Show Gist options
  • Save ethaizone/4a68217c10268f27cce9 to your computer and use it in GitHub Desktop.
Save ethaizone/4a68217c10268f27cce9 to your computer and use it in GitHub Desktop.
Example view render as helper
<?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