Simple Rails-like templating for CodeIgniter
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); | |
class Layout | |
{ | |
// The CodeIgniter instance | |
static var $ci; | |
// Name of out layout file | |
var $layout; | |
function __construct($layout = "default_layout") | |
{ | |
$ci =& get_instance(); | |
$this->layout = $layout; | |
} | |
/** | |
* The layout may differ per controller. If this is the case and the | |
* library is already autoloaded, we can use this function to change | |
* the layout that's loaded for that controller group. | |
*/ | |
function setLayout($layout) | |
{ | |
$this->layout = $layout; | |
} | |
/** | |
* @param view The view file to be loaded into the template. | |
* @param data Data accompanying the view; conventionally the $data array. | |
* @param suppress Returns the Layout as a string; for AJAX, maybe. | |
* @return nothing or the string of HTML if `suppress` is true | |
*/ | |
function view($view, $data=null, $suppress=false) | |
{ | |
$sub_view['content_for_layout'] = $ci->load->view($view, $data, true); | |
if ($suppress) | |
{ | |
$output = $ci->load->view($this->layout, $sub_view, true); | |
return $output; | |
} | |
else | |
{ | |
$ci->load->view($this->layout, $sub_view, false); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment