Simple Example of CodeIgniter Custom Layout

In this tutorial I will describe how to create layout for CodeIgniter. We have twig library to get layout method but if you want define your templates variables like title, content area and website description, the you need to create you custom layout.

In CodeIgniter you need to do below things:

1- Create template file and put into lib folder
2- Include this library file in Code Igniter config file when the application load.
3- Create layout file as a master page for application
4- Call view in controller file.

Step 1:
First step we will create template class for layout. Before creating calls we need to think about requirement of layout, there are two thing one is content area and second one is template variable. Here we will take a template variable which will keep all template variables. Template is key value pair variable where key is the content area of template.

We will create class file:

Above file put in CodeIgniter lib folder.

Step 2: Include this file into application, so for this put below line in config/autoload.php file.
$autoload['libraries'] = array('template');

Step 3: Created template.php file and put in views/layouts folder:

Step 4:Now we will Create action method in controller to render view file and set all variable in template layout file.

Here terms are:

Template : layout name
Contents : layoput Content area
Home : view file anme
Data : The parameters of file

Step 5: Created home.php file into views/ folder to view data.

2 thoughts on “Simple Example of CodeIgniter Custom Layout

Leave a Reply

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