Kohana Layout Module
It’s frustrating that Kohana lacks the same important feature that CodeIgniter does too. A layout system.
Back in the days I wrote a layout library for CodeIgniter, but the lack of modules made it rather difficult.
When I saw that Kohana lacks a layout too, I had the chance to port my CodeIgniter layout onto this platform and of course upgrade it a little.
I’ll write down what I think are the problems with Kohana View and what my Layout module does:
First of all, I saw a Template Controller in Kohana. View logic doesn’t suppose to be in the controller. Second, it’s a little confusing how to actually create partial views (views in views). The current Kohana View just passes data to the view files, I added a little logic of adding CSS and Javascript.
I lastly, what’s most frustrating is that each time I create a controller I have to rebuild all the View logic, the partial views, etc.
My solutions to the problems:
The Layout module extends the View_Core class, therefore having all the methods and properties of the View_Core.
It pulls out some additional structuring data to a configuration file. This helps to automatically rebuild all the partial view logic in each controller, with just a simple line.
$layout = new Layout('default');
The argument ‘default’ denotes the group of the particular layout. You can create different building logic for let’s say an administration panel or for a blogging system which requires different View elements.
Also the Layout module supports CSS and Javascript routing, which means that we can add CSS / Javascript to our views by reference, the actual paths to the files are centralized in the configuration file.
Another feature that I recently implemented (so it’s currently in alpha stage) that of combining the asset files (css, javascript) and routing through a combine controller. Like this:
PHP:
$layout->add_css('reset','fonts','about');
$layout->set_combine_css(true,'http://my-project.dev/combine/css');
You can define for the set_combine_css method the URL through which the assets are requested. This would be good if you want to add minifying and strip comments, or just want to use your own combining methods.
This would be included in the HTML file as follows:
<link href="http://my-project.dev/combine/css/public/css/reset.css,/public/css/fonts.css" media="screen,projection" rel="stylesheet" type="text/css"/>
Currently the combining is made in the Combine Controller, but eventually I will pull this out into a library in the Layout module.
How to set up the Layout Module:
You will need:
- Kohana framework, I didn’t included in my archive
- Layout Module – http://playground.primalskill.com/kohana-layout/
Create a fresh install of Kohana and copy the files with all the folders from the kohana_layout_module.zip
Now you can start examining the layout class. It’s in modules/layout/libraries/layout.php
The configuration file is in modules/layout/config/layout.php
The combine controller is in modules/layout/controllers/combine.php
Some additional sample files you can find at the application folder. (controllers, views)
If you have any question please leave a comment and I will respond. I hope you like this little module. New features will coming very soon.
If you enjoyed this article then help spread the word and please follow us on Twitter or subscribe to our RSS feed.
Subscribe to our RSS feedFollow us on Twitter
on Saturday 27, 2008
Hi!
Scripts has an error in tpl/main.php. Variable $js must named – $_js so that block will show.
on Saturday 27, 2008
Thanks.
on Saturday 27, 2008
I think Template_Controller in kohana2.2 already sovle the problem,what your think?
on Saturday 27, 2008
Well, not really. It seems to me that it is unfinished.
You cannot include javascript and css from PHP, you have to build the layout on each page (for ex. header and footer always remains constant)
on Saturday 27, 2008
the view logic problem easily can be solved by creating one helper with methods that return the constant views
on Saturday 27, 2008
I get the Fatal error: Cannot redeclare class View_Core in D:\ESPHP\htdocs\E-Study\system\libraries\View.php on line 13
on Saturday 27, 2008
Hmm, interesting. I never got that error message before.
The problem is with your Kohana View library and not the custom Layout library.
Did you modified the View_Core class?
Can you please tell me the version number of Kohana you’re using, so I can make some tests…
on Saturday 27, 2008
I also get the “Cannot redeclare class View_Core” error using Kohana v2.3.4 Release without modification.