[eluser]Phil Sturgeon[/eluser]
As per requested in your PM:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter Template Class
*
* Helps create pages easier with more options than load->view()
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author Philip Sturgeon
* @link
*/
class Template {
var $CI;
var $filename = '';
var $directory = '';
var $title = '';
var $data = array();
// Set default blocks
var $blocks = array('navigation', 'to-do');
var $header = 'header.php';
var $footer = 'footer.php';
var $wrap = true;
/**
* Constructor - Calls the CI instance and sets a debug message
*
* The constructor can be passed an array of config values
*/
function Template()
{
$this->CI =& get_instance();
log_message('debug', "Template Class Initialized");
if($this->directory == '' && $this->CI->uri->router->directory != ''):
$this->directory = $this->CI->uri->router->directory;
endif;
}
// --------------------------------------------------------------------
/**
* Set the mode of the creation
*
* @access public
* @param string
* @return void
*/
function create($filename = '', $data = array(), $return = false)
{
$this->CI =& get_instance();
if($filename != '') $this->filename = $filename;
if($data != array()) $this->data = $data;
// Work out the basics the template will need
$this->_set_defaults();
// Want header and footer file included, or just the main file?
if($this->wrap === true):
$output = $this->CI->load->view($this->directory.$this->header, $this->data, true);
$output .= $this->CI->load->view($this->directory.$this->filename, $this->data, true);
$output .= $this->CI->load->view($this->directory.$this->footer, $this->data, true);
else:
$output = $this->CI->load->view($this->directory.$this->filename, $this->data, true);
endif;
// Want it returned or output to browser?
if($return):
return $output;
else:
// Send it to output
$this->CI->output->set_output($output);
endif;
}
/**
* Set the title of the page
*
* @access public
* @param string
* @return void
*/
function title($title = '')
{
if($title != '') $this->title = $title;
}
function directory($directory = '')
{
if($directory != '') $this->directory = $directory;
}
/**
* Should we include headers and footers?
*
* @access public
* @param string
* @return void
*/
function wrap($wrap = true)
{
$this->wrap = $wrap;
}
/**
* Set some basic variables to be available to all pages
*
* @access public
* @param string
* @return void
*/
function _set_defaults()
{
$this->CI =& get_instance();
$this->CI->load->library('session');
$this->CI->load->helper('inflector');
// Replace all underscores with spaces, then capitalise first letters of words
$defaultTitle = humanize($this->CI->uri->router->class .' > '.$this->CI->uri->router->method);
// Set the basic defaults
$this->data['pageTitle'] = ($this->title) ? $this->title : $defaultTitle;
if($this->blocks != array()):
$this->data['blocks'] = $this->_build_blocks();
endif;
$this->CI->load->library('permission');
if(is_object($this->CI->permission)):
$user_result = $this->CI->permission->check();
$this->data['if_logged_in'] = ($user_result) ? array(array()) : array();
$this->data['if_not_logged_in'] = (!$user_result) ? array(array()) : array();
$manager_result = $this->CI->permission->check('manager');
$this->data['if_manager'] = ($manager_result) ? array(array()) : array();
$this->data['if_not_manager'] = (!$manager_result) ? array(array()) : array();
endif;
}
/**
* Set the mode of the creation
*
* @access public
* @param array
* @return array
*/
function _build_blocks()
{
$blockArray = array();
foreach($this->blocks as $blockName):
$block_data = array();
include(APPPATH.'views/blocks/'.$blockName.'_code.php');
$this->data = array_merge($this->data, $block_data);
$blockContent = $this->CI->load->view('blocks/'.$blockName, $this->data, true);
$blockArray[] = array('name' => $blockName, 'content' => $blockContent);
endforeach;
return $blockArray;
}
}
// END Template class
?>
The blocks system is VERY un-finished but atm all you need is a view file in the /blocks/name.php and a file /blocks/name_code.php. This PHP needs to create a variable called $block_data which will be called by this function.
Its far from perfect but if you cant be bothered with the blocks, just make the $this->blocks array empty and it shouldnt do anything.
Example:
Code:
$this->template->title('Hello!');
$this->template->create('pagename', $data);
Also, if your controller is in a sub directory, it will automatically look for a header and footer file in the sub directory. Guess I should clean this lib up and release it sometime.