Phil Sturgeon

Web developer, kayaker, outdoors madman and part-time alcoholic.


CodeIgniter Template library

Posted CodeIgniter at Dec 29, 2009

Working with multiple views in CodeIgniter has always been a pain. Out of the box CodeIgniter provides no real way of having a layout file and the only way to get header/footer functionality is to put $this->load->view('header') in your views or in each controller and method.

Obviously as a programmer I am lazy and don't want to do that. Years ago I made some dodgy helper which evolved into a library which I called CodeIgniter Layout library. The problem was this was a library that got used for loads of different applications, some modular, some not and ended up with a really confused set of features that were plain bodged together. It was so embarresingly screwed that I never released it, even though it is running 95% of all views in PyroCMS v0.9.7.

During PyroCMS v0.9.8 development one of the first things to go was the Layout library, which has been entirely re-coded with my CodeIgniter Template library.

This library has an awesome featureset:

So that's lots of crazy stuff that to me has been very useful and should be for others too.

The way it works is simple, you tell it what view you would like to load and tell it which layout to use, then it will wrap your view file with the layout suggested. By using the MY_Controller trick, you can set the name of the layout file for your entire application and by creating a Front_Controller and Back_Controller you can set different default layouts for your frontend or backend respectively. This is a big change for people not used to structuring their applications this way, but trust me it is worth it for several reasons I will go into in a later post.

Read more about this library to learn how it all works and see some code examples.

Comments

User comments
  • Gravatar http://philsturgeon.co.uk/

    Jun 30, 2010

    Robert K: There is no relation between my Template library and Colins Template library. We both made a library that handles Templates and neither of us are very imaginative at naming our projects.

    I guess the new trendy thing to do is give it some random name like "Tempala" or some crap, but I prefer the "does what it says on the tin" approach. :-)

  • Gravatar Robert K

    Jun 28, 2010

    Hi Phil, Can you add a note to your blog post clarifying the relationship (or lack thereof) between your library and the "CodeIgniter Template" library found at http://williamsconcepts.com/ci/codeigniter/libraries/template/index.html ?

    Thanks!

  • Gravatar Apad

    May 10, 2010

    Hi, great CMS you got there, i've tried and it's work, unless I still couldn't find how to assign different layout for main page, module pages and news page respectively.

    Thanks Phil, great work you've done there. I also had several third party modules created (Google maps integration, adsense code and PDF files upload)

  • Gravatar David Arnold

    Apr 20, 2010

    Hey there! This works great, however I would like to modify the path to something like "root_directory/public/themes/". When I set the theme_locations path it doesn't give me an error but it can't seem to find the themes. What am I doing wrong?

    thanks!
    -d

  • Gravatar Victorarias

    Mar 18, 2010

    Hi, very interesting, views on CodeIgniter is confusing and tedious at first, I'm very interested to know your technique.

    The Read more link is broken ...

  • Gravatar Monkeytail

    Dec 29, 2009

    Nice concept! Other than providing docs about the api.. could you give me/us a basic 'how-to' to make the benefits and workings (application level) of your Template lib (more) clear.

Post a comment