Posted by & filed under CodeIgniter, PHP.

A general website can be divided into 5 parts

  1. Header
  2. Content Area
  3. Left Sidebar
  4. Right Sidebar
  5. Footer

When developing in CodeIgniter, we often have these parts saved as separate views and load them each time a page is loaded, like in the code below:

function home(){
 $this->load->view('header_view');
 $this->load->view('home_view');
 $this->load->view('footer_view');
}

Although this is a good process, it is surely tedious for the developer. Nobody enjoys writing the same statements again and again.
Wouldn’t it be easier if the header and the footer would be included automatically?

There are multiple ways to do this. We’ll discuss them below one by one.

Method 1:

Create a function called template in each controller as shown below:


function template($template,$data){
$this->load->view('header_view');
$this->load->view($template,$data);
$this->load->view('footer_view');
}

But this is not a very good practice. Its again a pain to write this function in each controller. To make it slightly better, we use the approach in the following post. We’ll create a CodeIgniter helper to load our views for us.