This post while relevant is a little out of date, check out the newest post

I'm a huge fan of Codeigniter. Like a lot of developers, I used to think that if I didn't write the code myself, it's not worth me using it - Codeigniter changed that for me. Codeigniter's got lots of strengths, but also quite a few weaknesses. In my opinion, its biggest weakness is the way views are ouput. If you want a header and footer on every page, you're going to struggle by default (as evidenced by the number of forum threads and questions in IRC).

There are multiple ways to do it, including using php_include for the header/footer files in your views using PHP. Some other options are to load the header view, then your content, then your footer view in each controller method. As you can imagine, this is a lot of code duplication, and would be difficult to update in future. The solution I used for a while was to create a container view that loaded the header and footer on either side of a variable $content. Then, in the controller I loaded the view I needed into $data['content'] by setting the third parameter to true, and passed it to my container view. Again, this was more duplication than I liked. I took it one step further and wrote a helper that took a view name and data as parameters, and loaded the container view like I needed.

This was alright for a while, but I soon thought of a better way to do it. As I needed to extend the base Controller to handle some authentication, I could load the header in MY_Controller's constructor. I could also use _output() in that class to load my footer. Perfect! All I needed to do was to load the view I wanted and it'd automatically be framed. I didn't get chance to try this method however, as I realised how rigid my layout would have to be to fit inside and decided to bite the bullet and write my own theme management class.

My class handles the loading of multiple partials, and they are all available right up until they are output to be edited or reordered. It's hard to explain in words, so here's an example:

(Assuming theme is autoloaded)

In the MY_Controller constructor:

$this->theme->registerHeader('layout/header');
$this->theme->registerFooter('layout/footer');

In MY_Controller _output():

echo $this->theme->outputPage();

Partials are registered in almost the same way, except the first parameter is the section identifier (set to enable manipulation later) e.g.

$this->theme->registerPartial($name, $viewFile, $data, $position, $nodeBeforeAfter);

In the test controller:

$data['key'] = 'val';
$this->theme->registerPartial('first_s', 'test_section_one');
$this->theme->registerPartial('third_s','test_section_three', $data);
$this->theme->registerPartial('second_s','test_section_two', '', 'before', 'third_s');
$this->theme->registerPartial('fourth_s','test_section_four', $data, 'after', 'second_s');

This would output the views: layout/header, then test_section_one, test_section_two, test_section_four test_section_three, layout_footer on the page in order.

This is just a very basic example of how it can be used. I've put the theme class on github for everyone to see + use. It still needs documenting, but it's not too hard to work out how to use it given the examples above.

Feel free to, and please do use/fork the library and improve it. If you make any changes, let me know and I'll pull them into the master so that everyone can benefit from them.

Get The Library

Related posts:

  1. HowTo: Codeigniter Partial Library
  2. Codeigniter Partial Library
  3. iTunes Library Parser