Creating Header and Footer Template in Codeigniter

When you are creating/developing large websites/applications you need to fragment your web-page in sections like navigation menu, header and footer. The navigation menu, header and footer usually stay same on all pages of your website/application. If you don’t use sections for your web-page and if you want to change something like logo in header, add link in navigation menu or add/edit description in footer then you have to change on all pages manually.

PHP already have solution for this problem with the include() method. With this method you can include any file within another file.

In Codeigniter, you can create custom method in Loader class and call group of views. Codeigniter have Loader Class, initialized automatically by the system, as the name of class, is used to load elements like libraries, helpers, models, view files or your own files.

Steps to follow:

  1. Create new php class extends CI_Loader in application/core directory

2. Create a method inside MY_Loader class named template

In this template method we have 3 parameters:

Now we call 3 views in this method, first for header, main template and then footer and return result if we want to show view as data otherwise this will show all called views in sequence.

Final code looks like: