I’ve seen several people ask about CodeIgniter and templates to avoid a lot of copy code. Unfortunately they do not have a template library but this can be easily overcome.

In the view I like to create an includes folder that contains my main template file, the header file and the footer file:

within the header you write all the nesacarry code such as the doctype, head and even parts of the body that are generic just like:

Do the same inside the footer file:

inside the main template file “sampleTemplate.php” you need the following code:

This file will load the header, take in a variable that contains the main_content and then load the footer.

Now you have the pieces in place to load a view that contains the main_content. ¬†Create a new view and add the main_content code there, here is an example (“my_main_content_view.php”):

In order for your view to be loaded you need to do the following within your controller:

The first line loads the file within the view into a variable. The second line loads the template view and passes the main_content variable.

These steps allow you to implement an easy template system within CodeIgniter.