Templating With CodeIgniter
I started working on the template system today and thought I would share some of things I am doing. One of the features I wanted was to have an easy template system and it must be flexible enough to not choke if a certain template body file couldn’t be found. Another thing I like to do is have one global layout file and include the body portion where needed. This way you do not have to worry about a header, footer, etc.
![]()
By default CodeIgniter places all template files in system/application/views/ which is fine for most but I wanted a directory out side “system” to hold our template files. My thoughts are beginners or people with out any CodeIgniter experience would not know where to look. After a bit of research I found this post and after making the needed changes it seemed to work flawlessly. Hopefully this will be the only core hack I have to make. I am a firm believer in only hacking what is minimally required to get the job done.
Now it was time for the model. For 68 knowledge base we will need a model that gets all the needed settings out so it will have to be called in every controller. Since this model will be used everywhere I can add the display template function inside it.
Here is the code to start the model:
class Init_model extends model
{
function Init_model()
{
parent::Model();
$this->obj =& get_instance();
}Now that we have the model I added two more functions:
function _testexists($body)
{
$file = KBPATH.'templates/'.$body.'.php';
if (!file_exists($file))
{
return false;
}
return true;
}
function displayTemplate($template, $data)
{
$data['settings']['template']='custom';
$body_file='front/'.$data['settings']['template'].'/'.$template;
if($this->_testexists($body_file))
{
$data['body'] = $this->load->view($body_file, $data, true);
}
else
{
$data['body'] = $this->load->view('front/default/'.$template, $data, true);
}
$this->load->view('front/'.$data['settings']['template'].'/layout.php',$data);
}This will be called from the controller like this:
$this->init_model->displayTemplate('article', $data);What this does is call the displayTemplate function which in turn tests to see if the template file actually exists and if not revert back to the default template set.
Wrap Up
Hopefully by doing it this way users can create custom themes without having to use each and every view file in their theme folder. Also keep in mind this code is just a demonstration on how I am doing it and a lot of code has been stripped out to make it easier to follow. You can download my full init_model file below.
October 28, 2007 6 Comments


6 comments
[...] Templating with CodeIgniter, templating study [...]
[...] Templating with CodeIgniter [...]
[...] Templating with CodeIgniter [...]
Hi,
Love the elegance and simplicity of the view theme/view model.
Great stuff
hmh looks like
mine worked only with begining slash.
$this->_ci_view_path = PUBPATH.’/views/’;
Hi,
I would like to use your templating system but i can’t find the download file. Can someone post it in the comments?
Leave a Comment