Created
July 25, 2013 20:50
-
-
Save igbanam/6083635 to your computer and use it in GitHub Desktop.
Simple Rails-like templating for CodeIgniter
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
<?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