Using Template in Codeigniter

This tutorial will show you an example on how to use template in Codeigniter. In an web application a template is recommended because there are certain regions or areas which are common to all pages and only the body portion of the page gets changed. Common areas like header, footer, left sidebar or right sidebar are mainly common areas for most of the web applications. So we will fix those common areas in the template file so that every page will use those common areas in the application and body portion of each page will contain content which is specific to the page.

We will use here Colin Williams template library to demonstrate the template system in Codeigniter. Now we will see step by step process how can we use the template library in Codeigniter application.

If you want to use Template library with Codeigniter 3 then you can read Using Template in Codeigniter 3

Step 1. Put this below template.php file under <project root>/application/config/ directory

We have defined two regions title and content because those two regions will be changing according to the page content. You can also define other regions if you want them to be updated according to the page content. We have used the Codeigniter’s default parser to parse the template regions. We have used parse_template to TRUE to parse the template.

Step 2. Now put this below template.php template library under <project root>/application/libraries/ directory. If you look at the below template library file you will see I have added extra regions, i.e., ‘_meta’ => array() which will contain meta tags for the page. I have also added the below code snippets so that each page can contain meta tags specific to that page only.

Template library file.

Step 3. So we need to load the template library only once and the template library will be used in subsequent requets. So autoload it using autoload.php configuration under <project root>/application/config/ directory.

Step 4. Create a template view file template.php and put it under <project root>/aplication/views/template/ directory. The below file is self explanatory if you see the comments.

Step 5. Now we will see how to use the template file. Create a home.php controller file and put it under <project root>/application/controllers/ directory.

The $data[‘home’] variable contains the text which will be shown in home.php file using $home variable as shown in below step.

Step 6. Create a home.php view file and put it under <project root>/application/views/ directory.

Step 7. Now change the default controller in routes.php file.

Step 8. Now run the application you will see the expected output.

Download the source code from template.

Thanks for your reading.

Soumitra Roy Sarkar

I am a professional Web developer, Enterprise Application developer, Software Engineer and Blogger. Connect me on Twitter Facebook  Google Plus Linkedin

9 Replies to “Using Template in Codeigniter”

  1. hi!

    i just try your script and then got an error message:

    “Fatal error: Call to undefined method MY_Loader::_ci_load_class() in /Applications/MAMP/htdocs/aff/application/third_party/MX/Loader.php on line 158”.

    How can i solve that?

  2. I use codeiniter 2.2.6. When complete setting and run the app,
    the screen appear error:
    Fatal error: Class ‘Public_Controller’ not found in C:\Program Files\VertrigoServ\www\CodeIgniter-2.2.6\application\application_user\controllers\home.php on line 11

    I don’t know in the ‘home’ controller when the script “class Home extends Public_Controller” where is “Public_Controller”?

    Ps: when I try setting with codeiniter 3.1.6 it occur the same error

Leave a Reply

Your email address will not be published. Required fields are marked *