Simple Template Library

Derek Jones edited this page Jul 5, 2012 · 25 revisions
Clone this wiki locally

Category:Library::Template Engine | Category:Library::Community

This is a small template class that I am currently using on a university project.

Setup

Open config/autoload.php and add template as shown below.

$autoload['libraries'] = array('template');

Source

application/libraries/Template.php


<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Template Class
 *
 * Template View Parse Class
 *
 * @package     CodeIgniter
 * @subpackage  Libraries
 * @category    Templates
 * @author      Koola
 * @link
 */
class Template {

    /**
     * Constructor
     *
     * @access    public
     */
    function __construct()
    {
        log_message('debug', "Template Class Initialized");
    }
    
    // --------------------------------------------------------------------
    
    /**
     * Load template
     *
     * @access   public
     * @param    String
     * @param    Array
     * @param    Array
     * @param    bool
     * @return   parsed view
     */
    function load($template = '', $view = array(), $vars = array(), $return = FALSE)
    {
    $this->CI =& get_instance();
    $tpl = array();
        
    // Check for partials to load
    if (count($view) > 0)
    {
        // Load views into var array
        foreach($view as $key => $file)
        {
            $tpl[$key] = $this->CI->load->view($file, $vars, TRUE);
        }
        // Merge into vars array
        $vars = array_merge($vars, $tpl);
    }
    
    // Load master template
    return $this->CI->load->view($template, $vars, $return);
    }
}
?>

application/views/about.php


<h1>Small Template Library</h1>

<p>You just called your first template!</p>

<p><br />Page rendered in {elapsed_time} seconds</p>

application/views/main.php


&lt;html&gt;
&lt;head&gt;
&lt;title&gt;&lt;?=$title;?&gt;&lt;/title&gt;
&lt;style type="text/css"&gt;
body {
 background-color: #fff;
 margin: 40px;
 font-family: Lucida Grande, Verdana, Sans-serif;
 font-size: 14px;
 color: #4F5155;
}
h1 {
 color: #444;
 background-color: transparent;
 border-bottom: 1px solid #D0D0D0;
 font-size: 16px;
 font-weight: bold;
 margin: 24px 0 2px 0;
 padding: 5px 0 6px 0;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

<div id="header">Header</div>
<div id="content">&lt;?=$content;?&gt;</div>
<div id="footer">Footer</div>

&lt;/body&gt;
&lt;/html&gt;

application/controller/home.php


&lt;?php

class Home extends Controller {

    function __construct()
    {
        parent::Controller();    
    }
    
    function index()
    {
        $data['title']  = 'My page title';
        
        $partials = array('content'=>'about');

        $this->template->load('main', $partials, $data);
    }
}
?&gt;

Usage

Calling multiple views is easy:

$partials = array('content'=>'article','head'=>'header','footer'=>'footer');

Key in the array above is the PHP var (i.e. <?=$content;?> ) that should match the var in your master template. Value is the filename to call from your views directory.

$this->template->load('main', $partials, $data);

Main in the above code is the Master Template file. Partials is view array. Data passes array data just like a view call.