CodeIgniter Template library

Posted: 2009-12-29
Category: CodeIgniter

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:

  • Page title - will guess if nothing is set
  • Breadcrumbs
  • Meta-data
  • Layout files - wrapper around the loaded view
  • Themes
  • Partials - Give any nubmer of views a name and load them in the layout (sidebar, menu, extra footer, etc)
  • Loading views in modules - supports Modular Separation (PHP 5) and any system that uses $this->router->fetch_module().

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

Gravatar
Monkeytail

2009-12-29

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.

Gravatar
Victorarias

2010-03-18

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
David Arnold

2010-04-20

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
Apad

2010-05-10

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
Robert K

2010-06-28

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

2010-06-30

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
Zak

2010-09-10

If you were a Burner you could call it Templaya....

Gravatar
Thephpx

2011-02-23

Hi,

Don't know if this will add much value to this great library but I have created a helper function in CI that allows developer to dynamically assign and call view partials. No need to manually editing the layout to add partials, just a single helper call does it all. Also added template region support, now region specific partial loading also supported.

link: http://bit.ly/ijFPTz

comments and suggestions are welcome :)

thanks,

thephpx

Gravatar
Johnmerlino

2011-02-27

Does anyone here have an example of how to use this? THat is, what you put in the controller to render both a view and a partial in the template?

Thanks for response.

Gravatar
Rj

2011-03-13

Phil -
This library doesn't want to autoload in the latest v. of Reactor. I have to set the following in my public_controller:

$this->load->config('template');
$this->load->library('template', $this->config->item('theme_locations'));

Using modular extensions. Any idea why it won't autoload?

Gravatar

2011-03-13

You don't need to load the config file itself, that is done automatically by the Loader library, which uses the config files to fetch parameters for the constructor. You are stopping it from looking for the file because you are populating the constructor parameter with a different array.

Just use $this->load->library('template') and it should load fine.

Gravatar
Rj

2011-03-13

I'm not sure where I read you needed to add the template config file to the config autoload array; I appreciate the response, it helped tremendously.

Glad to see your committing to the framework now.

Thanks

Gravatar
Nermin

2011-05-03

Hi Phil

is this library still alive, I get

Page missing for this link http://philsturgeon.co.uk/code/codeigniter-template

Where can I download it for codeigniter?

Gravatar
Sarvar

2011-05-16

Inside PyroCMS... This CMS used template library...

Posting comments after three months has been disabled.