Templates & themes for CodeIgniter
One of the biggest drawbacks of a framework is the absence of built in theming functionality. CodeIgniter is no exception to that rule. Although there are quite a few libraries out there which aim to provide theming support, they don’t feel like being part of the framework. I was annoyed with having to use anything which looks like an ugly hack so I created a library which supports themes in CodeIgniter in a native style.
First of all let’s see how this baby works:
Using themes:
$this->load->theme('demo'); $this->load->view('home');
Loading CSS and Javascript files from within your views:
$this->load->css('/themes/demo/css/styles.css'); $this->load->js('/themes/demo/js/javascript.js');
Yes that was all. It is that simple.
Structure of your files
In the same path as your application folder create a theme folder and inside it a folder with the theme’s name. Also create a folder with the theme’s name inside views.
Inside the download you will see how the demo theme is structured.
/application/views/demo/... (Your views go here) /themes/demo/css/... (CSS files) /themes/demo/js/... (Javascript files) /themes/demo/theme.php (The theme file itself)
Bonus: Caching control
Have you noticed that sometimes when you update your css or javascript files the changes don’t take effect until the browser empties it’s cache ? This library supports a suffix for all css and javascript files which allows you to control the caching of your files by the browser. Inside the config folder you will find a file mainframe.php. Make sure you autoload it and set a version for your files.
$config['cjsuf'] = '0.1';
When you change that version to say 0.2, browsers will be forced to reload them since they think it is a different file.
Inside the download there is a demo theme to get you started