Skip to content

Simple Template Library

World Wide Web Server edited this page Jul 4, 2012 · 25 revisions

Category:Libraries Category:Libraries::Template Engines This is a small template class that I am currently using on a university project.

[h3]Setup[/h3]

  1. Create [b]Template.php[/b] into [b]system/libraries[/b]

  2. Open [b]config/autoload.php[/b] and add 'template' and 'parser' to the array: [code]$autoload['libraries'] = array('template'.'parser');[/code]

[h3]Source[/h3]

[h4]Template[/h4] [code] <?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 http:// */ class Template {

    var $tpl = array();

    /**

    • Constructor
    • @access public */ function Template() { 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();

      // Load views into var array foreach($view as $key => $file) { $tpl[$key] = $this->CI->load->view($file, NULL, TRUE); } // Merge to var output array $output = array_merge($vars, $tpl); // Parse template return $this->CI->parser->parse($template, $output, $return); } } ?> [/code]

[h3]Views[/h3]

[h4]about[/h4] [code]

Small Template Library

You just called your first template!


Page rendered in {elapsed_time} seconds

[/code]

[h4]main[/h4] [code]

Small Template Library

You just called your first template!


Page rendered in {elapsed_time} seconds

[/code]

[h3]Controllers[/h3]

[h4]home[/h4] [code] <?php

class Home extends Controller {

function Home()
{
    parent::Controller();    
}

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

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

} ?> [/code]

[h2]Operation[/h2]

Clone this wiki locally