Using Template in Codeigniter 3

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-a-days the Williams Codeigniter Template library is no longer being supported or improved, the last version produced was 1.4.1. So to make it work with Codeigniter I have done a little modification to it.

We will use here the same application as I have shown in but the only thing change here is I will use Template library instead of loading the view file.

If you want to use Template library with Codeigniter 2 then you can read
Now we will see step by step process how can we use the template library in Codeigniter application.


XAMPP in Windows
Netbeans 8.1
Codeigniter 3.0.6

Configure XAMPP and Netbeans

From Netbeans IDE go to Tools->Options. Click on PHP. Now on tab “General” browse the file for “PHP 5 Interpreter”. The php interpreter file generally placed inside the <physical drive in Windows OS>:xamppphpphp.exe

Configure Codeigniter and Netbeans

Create a new PHP project in Netbeans. Then remove the index.php file from the newly created project. Now copy the extracted files from Codeigniter 3.0.6 to the newly created project directory.

Setup HMVC with CodeIgniter and Example of HMVC in action

Please read

Step 1. Create a template.php file under application/config directory with below source code. This is the template configuration file used define as many as template view files with different regions. These regions can be replaced by dynamic content for each page. Here we are using only one template as a view file for entire application so we have only one template group. If you need to know how to use multiple template files for an application then you can look at

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 we will see the template library file. Put the below source code in Template.php file under application/libraries directory. The original template class is little different from what I presented here because here I changed the class name from CI_Template to Template and also I have added one extra region _meta => array() for giving provision for every page to add meta key/value.

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

Also do the following

Step 4. Add below config to the file application/config/config.php

Step 5. Create an assets directory in parallel with application folder as shown in below figure.

Codeigniter template

Download here assets

Step 6. Create a template view file template.php under application/views/templates directory

Step 7. Create common regions view files header.php and footer.php files under application/views/templates/snippets directory



Step 8. Now we will change our application/core/MY_Controller.php file to use template

Step 9. Now change application/modules/site/controllers/site.php to use template

Step 10. Change the view file in application/modules/site/views/home.php

Step 11. Also change application/modules/signin/controllers/signin.php to use template

Step 12. Change the view file in application/modules/signin/views/signin.php

Step 13. Run the application.

Home page

codeigniter template home page

Signin Page

codeigniter template signin page

Thanks for 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

15 Replies to “Using Template in Codeigniter 3”

  1. i use it and that have error like this
    Parse error: syntax error, unexpected ‘$value’ (T_VARIABLE) in /var/www/html/mcsy/application/libraries/Template.php on line 561
    what this problem? can you help me?

  2. Suggestion: Change write_view method to:
    function write_view($region, $view, $data = array(), $overwrite = false) {

    Otherwise the foreach loop in Parser.php (line 144) will throw an error as it will be attempting to loop a NULL value.

  3. Hello,
    it’s very good template library, and i already used it in my codeigniter project.
    is there any github repo that i can contribute to with this template library?

  4. An Error Was Encountered
    Either you have not provided a master template or the one provided does not exist in D:\xampp5.6\htdocs\sjcollection\application\views. Remember to include the extension if other than “.php”

Leave a Reply

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