Source for file Template.php
Documentation is available at Template.php
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
* An open source application development framework for PHP 4.3.2 or newer
* @author ExpressionEngine Dev Team
* @copyright Copyright (c) 2006, EllisLab, Inc.
* @license http://codeigniter.com/user_guide/license.html
* @link http://codeigniter.com
// --------------------------------------------------------------------
* CodeIgniter Template Class
* This class is and interface to CI's View class. It aims to improve the
* interaction between controllers and views. Follow
* @link http://www.williamsconcepts.com/ci/libraries/template/index.html
* @copyright Copyright (c) 2008, Colin Williams.
var $regions = array('_scripts' => array(),
* Loads template configuration, template regions, and validates existence of
// Copy an instance of CI so we can use the entire framework.
$this->CI = &get_instance();
// Load the template config file and setup our master template and regions
include(APPPATH . 'config/template' . EXT);
// check if administration part is showing and select template
if ($this->CI->uri->segment(1) == 'admin') {
if($this->CI->myauth->check()){
$template_index = $this->CI->entity_model->selectOne('user','template',array('username'=> $this->CI->myauth->getUser()),TRUE);
if(isset ($template[$template_index])){
if(!$is_set) $this->set_template($template['active_template']);
// --------------------------------------------------------------------
* Use given template settings
* @param string $ array key to access template settings
if (isset ($this->config[$group])) {
show_error('The "' . $group . '" template group does not exist. Provide a valid group name or add the group first.');
// --------------------------------------------------------------------
* @param string $ filename of new master template file
$this->master = $filename;
show_error('The filename provided does not exist in <strong>' . APPPATH . 'views</strong>. Remember to include the extension if other than ".php"');
// --------------------------------------------------------------------
* Dynamically add a template and optionally switch to it
* @param string $ array key to access template settings
* @param array $ properly formed
if (! isset ($this->config[$group])) {
$this->config[$group] = $template;
if ($activate === true) {
show_error('The "' . $group . '" template group already exists. Use a different group name.');
// --------------------------------------------------------------------
* Get information about all templates
foreach($this->config as $index=> $template){
if (isset ($template['template']) && (file_exists(APPPATH . 'views/' . $template['template'] . 'template.' . $template['extension']))) {
// --------------------------------------------------------------------
* Initialize class settings using config settings
* @param array $ configuration array
if (isset ($props['template']) && (file_exists(APPPATH . 'views/' . $props['template'] . 'template.' . $props['extension']))) {
$this->master = $props['template'] . 'template.' . $props['extension'];
// $this->master_path = APPPATH .'views/'. $props['template'];
$this->master_path = base_url() . 'system/application/views/' . $props['template'];
// Master template must exist. Throw error.
show_error('Either you have not provided a master template or the one provided does not exist in <strong>' . APPPATH . 'views</strong>. Remember to set the extension');
if (isset ($props['regions'])) {
// Set parser and parser method
if (isset ($props['parser'])) {
if (isset ($props['parser_method'])) {
// Set master template parser instructions
$this->parse_template = isset ($props['parse_template']) ? $props['parse_template'] : false;
// --------------------------------------------------------------------
* Set regions for writing to
* @param array $ properly formed regions array
$this->regions = array('_scripts' => array(),
foreach ($regions as $key => $region) {
// Regions must be arrays, but we take the burden off the template
// developer and insure it here
// --------------------------------------------------------------------
* Dynamically add region to the currently set template
* @param string $ Name to identify the region
* @param array $ Optional array with region defaults
if (! isset ($this->regions[$name])) {
show_error('The "' . $name . '" region has already been defined.');
// --------------------------------------------------------------------
* Empty a region's content
* @param string $ Name to identify the region
if (isset ($this->regions[$name]['content'])) {
$this->regions[$name]['content'] = array();
show_error('The "' . $name . '" region is undefined.');
// --------------------------------------------------------------------
* @param string $ name of parser class to load and use for parsing methods
$this->CI->load->library($parser);
// --------------------------------------------------------------------
* @param string $ name of parser class member function to call when parsing
// --------------------------------------------------------------------
* Write contents to a region
* @param string $ region to write to
* @param string $ what to write
* @param boolean $ FALSE to append to region, TRUE to overwrite region
function write($region, $content, $overwrite = false)
if (isset ($this->regions[$region])) {
if ($overwrite === true) { // Should we append the content or overwrite it
$this->regions[$region]['content'] = array($content);
$this->regions[$region]['content'][] = $content;
// Regions MUST be defined
show_error("Cannot write to the '{$region}' region. The region is undefined.");
// --------------------------------------------------------------------
* Write content from a View to a region. 'Views within views'
* @param string $ region to write to
* @param string $ view file to use
* @param array $ variables to pass into view
* @param boolean $ FALSE to append to region, TRUE to overwrite region
function write_view($region, $view, $data = null, $overwrite = false)
unset ($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
foreach ($args as $suggestion) {
// Just change the $view arg so the rest of our method works as normal
$content = $this->CI->load->view($view, $data, true);
$this->write($region, $content, $overwrite);
// --------------------------------------------------------------------
* Parse content from a View to a region with the Parser Class
* @param string $ region to write to
* @param string $ view file to parse
* @param array $ variables to pass into view for parsing
* @param boolean $ FALSE to append to region, TRUE to overwrite region
function parse_view($region, $view, $data = null, $overwrite = false)
$this->CI->load->library('parser');
unset ($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
foreach ($args as $suggestion) {
// Just change the $view arg so the rest of our method works as normal
$this->write($region, $content, $overwrite);
// --------------------------------------------------------------------
* Dynamically include javascript in the template
* NOTE: This function does NOT check for existence of .js file
* @param string $ script to import or embed
* @param string $ 'import' to load external file or 'embed' to add as-is
* @param boolean $ TRUE to use 'defer' attribute, FALSE to exclude it
* @return TRUE on success, FALSE otherwise
function add_js($script, $type = 'import', $defer = false)
$this->CI->load->helper('url');
$filepath = base_url() . $script;
$js = '<script type="text/javascript" src="' . $filepath . '"';
$js = '<script type="text/javascript"';
// Add to js array if it doesn't already exist
$this->write('_scripts', $js);
// --------------------------------------------------------------------
* Dynamically include CSS in the template
* NOTE: This function does NOT check for existence of .css file
* @param string $ CSS file to link, import or embed
* @param string $ 'link', 'import' or 'embed'
* @param string $ media attribute to use with 'link' type only, FALSE for none
* @return TRUE on success, FALSE otherwise
function add_css($style, $type = 'link', $media = false)
$this->CI->load->helper('url');
// $filepath = base_url() . $style;
$css = '<link type="text/css" rel="stylesheet" href="' . $filepath . '"';
$css .= ' media="' . $media . '"';
$css = '<style type="text/css">@import url(' . $filepath . ');</style>';
$css = '<style type="text/css">';
// Add to js array if it doesn't already exist
$this->write('_styles', $css);
// --------------------------------------------------------------------
* Render the master template or a single region
* @param string $ optionally opt to render a specific region
* @param boolean $ FALSE to output the rendered template, TRUE to return as a string. Always TRUE when $region is supplied
* @return void or string (result of template build)
function render($region = null, $buffer = false, $parse = false)
$this->write_view('menu', 'menu', array('menu' => $this->CI->menu->getMenu()));
// Just render $region if supplied
if ($region) { // Display a specific regions contents
if (isset ($this->regions[$region])) {
$output = $this->_build_content($this->regions[$region]);
show_error("Cannot render the '{$region}' region. The region is undefined.");
// Build the output array
foreach ($this->regions as $name => $region) {
$this->output[$name] = $this->_build_content($region);
// Use provided parser class and method to render the template
($this->master, $this->output, true);
// Parsers never handle output, but we need to mimick it in this case
$this->CI->output->set_output($output);
// Use CI's loader class to render the template with our output array
$output = $this->CI->load->view($this->master, $this->output, $buffer);
// --------------------------------------------------------------------
* Load the master template or a single region
* Use render() to compile and display your template and regions
function load($region = null, $buffer = false)
$this->render($region, $buffer);
// --------------------------------------------------------------------
* Build a region from it's contents. Apply wrapper if provided
* @param string $ region to build
* @param string $ HTML element to wrap regions in; like '<div>'
* @param array $ Multidimensional array of HTML elements to apply to $wrapper
* @return string Output of region contents
function _build_content($region, $wrapper = null, $attributes = null)
// Can't build an empty region. Exit stage left
if (! isset ($region['content']) or ! count($region['content'])) {
// Possibly overwrite wrapper and attributes
$region['wrapper'] = $wrapper;
$region['attributes'] = $attributes;
// Open the wrapper and add attributes
if (isset ($region['wrapper'])) {
// This just trims off the closing angle bracket. Like '<p>' to '<p'
$output .= substr($region['wrapper'], 0, strlen($region['wrapper']) - 1);
if (isset ($region['attributes']) && is_array($region['attributes'])) {
foreach ($region['attributes'] as $name => $value) {
// We don't validate HTML attributes. Imagine someone using a custom XML template..
$output .= " $name=\"$value\"";
// Output the content items.
foreach ($region['content'] as $content) {
if (isset ($region['wrapper'])) {
// This just turns the wrapper into a closing tag. Like '<p>' to '</p>'
$output .= str_replace('<', '</', $region['wrapper']) . "\n";
/* End of file Template.php */
/* Location: ./system/application/libraries/Template.php */
|