Skip to content

codeigniter template

Derek Jones edited this page Jul 5, 2012 · 12 revisions

Category:Library::Community

Overview

This tutorial is based on Phil Sturgeon's template and themeing library which can be found at:

http://bitbucket.org/philsturgeon/codeigniter-template/

Installation

After downloading the codeigniter-template package from BitBucket, unpack it to the correct library and config directory locations.

Example1: Basic Templating

  1. Open up application/config/autoload.php and add 'template' to the libraries array htat are being autoloaded. For example:
$autoload['libraries'] = array('database', 'template');
  1. Create the directory applications/views/base/ and add a layout.php file including a basic html layout such as:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
        &lt;?php echo $template['partials']['header']; ?&gt;
&lt;/head&gt;
    &lt;body&gt;
        &lt;?php echo $template['body']; ?&gt;
    &lt;/body&gt;
&lt;/html&gt;
  1. Create the directory applications/views/basic/partials and add a header.php file in there including your css and metadata stuff, for example:
&lt;title&gt;&lt;?php echo $template['title'];?&gt; | Example Site &lt;/title&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8" /&gt;
  1. Now in some global code (this could be a post_controller_constructor Hook or MY_Controller) add to the constructor the template configuration, for example:
    function __construct()
    {
        parent::Controller();
    
        $this->template->set_layout('layout');
        $this->template->enable_parser(FALSE); // default true
        
        $this->template->set_partial('header', 'partials/header', FALSE);
        
    }
  1. in your controller, if you are using MY_Controller you should extend MY_Controller not Controller. For the index function you should use this code to display your view:
        $this->template->build('body', $data);

where index is the view file application/views/index.php or if you are currently using a modular system like Modular Separation it will look in application/modules/modulename/views/index.php.

  1. the reference to the 'body' in the build() method above represents the view called body.php inside the module's views/ directory and so you should create it and add some content there

Contact

Liran Tal liran.tal@gmail.com For any updates, improvements and bugs

Clone this wiki locally