@ghumkumar

Random Thoughts

CodeIgniter

The thing I hate most about CodeIgniter is that the views folder become messed up after a while. So many files and so difficult to track down a view for a certain area of an application. So, to solve that I figured I should create a library to override the ways of managing views. I wrote this very small library to solve the problem. Now I can easily create different templates (or themes whatever you prefer them to call) inside the views. Also I’ve added a layout file (or you can call the master file) in the template for the base structure of the page. So, I don’t have to repeat the areas like header, footer or menu on every view. The library can be download the library from GitHub.

Here I’ve explained how the library works and how to integrate it within a CodeIgniter application.

The library

So, the library is simple consisting only three methods – get, set and view. They does exactly what they are called, get current template, set active template and loads the view respectively. Also I’ve defined the default template, which is used in case template is not set anywhere, at the top of the library.

define('DEFAULT_TEMPLATE', 'default');

You can set the default template in the config file if you like. Let’s jump into the library codes.

Remember, our session library must be loaded. I prefer to load it using autoload.php as I use the session a lot.

Finally in the view function, I’ve checked for the existence of the desired view within the template and if the file exists I’ve loaded the view within the layout (master file I’ve mentioned earlier) of that template.

Here I’ve used the $print variable to determine whether to load the view or not. Sometimes I want to handle AJAX calls which should not be sent to output.

Adding template

To add a new template we need to add a directory within the views directory. Every directory within the views are considered as a template. Inside each template there should be a layout.php file and all other views you need. You can put your views within sub directories if you want but the layout.php must stay right within the template directory. So, if I add a new template called mytemplate the directory structure will be like this:

|_ viewes
|_ mytemplate
|_ layout.php
|_ otherview.php

The layout file

The structure of layout.php depends on however the application looks like. We just need to use $this->load->view($view, $data); wherever we want to output our view’s content. So, a sample layout.php file might look like this:

Usage

Well, using the library is very easy. Once the library is placed inside the applications library folder and loaded in the controller (though I prefer using autoload.php), we can load a view using $this->template->view('view_name', $data); in the controller. Now, to organize the views properly we can make sub directories within the template. Let’s say we have an index view for the home controller and an account view for member controller. We can create two folder called home and member within the template folder and then add the views like home/index.php and member/account.php files. Then from the index function of home controller we should call the view like:

$this->template->view('home/index', $data);

And from the account function of member controller we should call the view like:

$this->template->view('member/account', $data);

That’s about it. Download the files from the GitHub repository and I’ve added a sample controller with sample template structure in there.