Business, Tech, Life, and Whatever else

Code Igniter Template Tutorial

by Eric Barnes on July 7, 2008

I have recently been using CodeIgniter and I will say it is an awesome framework to work with.

As I am jumping into creating my application I did come across a neat or useful way to structure your views so that you can allow the administrator of the site to select which template they want to use.

Before I jump in I do want to say that I create one layout.php file and this file is the whole layout which an include command for the body or content section.

First off with the default directory structure it looks like this:

/system/
/system/application/
/system/application/views/
index.php

Now what I do is create sub folders under the views folder like this:

/system/
/system/application/
/system/application/views/
/system/application/views/default/
/system/application/views/coolnew/
index.php

As you can see from that I now have two template directories default and coolnew.

Now I create a new model file. I name mine init_model.php and include this code:

<?php
class Init_model extends Model {

    function Init_model()
    {
        parent::Model();
		$this->obj =& get_instance();
    }

	function getSettings()
	{
		$this->settings['template']='default';
		return $this->settings;
	}

	function displayTemplate($template, $data)
	{
		$data['body'] = $this->load->view($data['settings']['template'].'/'.$template, $data, true);
		$this->load->view($data['settings']['template'].'/layout.php',$data);
	}
}
?>

Basically what this class does is when it is initialized it sets the template directory they want to use. I hard coded in default but this would normally come from the database. Next the displayTemplate function handles loading the correct template and body file. That way all your load->view is in one location and you can go back later and make any changes.

Inside my controller I use this code to call the view:

$this->init_model->displayTemplate('index.php', $data);

Subscribe Now

If you enjoyed this post, you will definitely enjoy my others. Subscribe to the feed to get instantly updated for those awesome posts soon to come.


  • Love it, I had to makes some changes to it to work with matchbox, but in general this was the easiest and quickest template parser that we ever imagined finding.

    Adding in some coding now to pull settings from a db table.

    Anyone needing the same email and we will send you a copy.
  • Billy glad it helped! Here is a full file from 68kb - knowledge base app - that may show how I used it with a settings table: http://code.google.com/p/68kb/source/browse/tru...
  • gdfgs
    ghjhf
  • Thanks for the comment. I also just found a nice little screen cast that uses a similar approach:
    http://screenr.com/YO7

    They don't use the model like I do but should give a good example of the idea.
  • Kai
    Thank you~ I needed to create a template system for my project and have been thinking of ways to do it. This is very straight forward and simple, perfect for what I need :)
blog comments powered by Disqus