Skip to content

Instantly share code, notes, and snippets.

@igbanam
Created July 25, 2013 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igbanam/6083635 to your computer and use it in GitHub Desktop.
Save igbanam/6083635 to your computer and use it in GitHub Desktop.
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