Please be aware that I no longer use this method. Please follow this link to see how I handle layout with Codeigniter
If you have ever worked with an MVC framework for web developent, you know that the “V” stands for “view”.
At work, I program in coldfusion, and use the coldbox framework, and I love it. At home, I write PHP code, with the codeigniter framework.
I love codeigniter, but I think that its biggest weakness is the view handeling, all thought codeigniter had taken strides to improve it. At the time of writing this, codeigniter is at version 1.6.1, and allows multiple views to be loaded at one time.
example:
<?php
class Page extends Controller {
function index()
{
$data['page_title'] = 'Your title';
$this->load->view('header');
$this->load->view('menu');
$this->load->view('content', $data);
$this->load->view('footer');
}
}
?>
This is great with one exception, codeigniter simple builds a stack of the view results, appending them to each other.
This means that in header.php, loaded at the start of the view sequence, we would have to opening tags, such as <body>, and then in the last view, footer, we have to close the tags we opened in header.php, in this case, </body>
I don’t know about you, but opening tags in open file, and depending on another file to close them is not a good practice. Using a MVC setup and then doing something like this is very counter intuitive.
To fix this, codeigniter needs to support layouts, as well as views.
A layout is a file that contains the framework for a page, and the views are included and rendered inside the layout. basically filling out the content of the page. This also leaves your code the ability to be more flexible. Your views are pluggable components that don’t care about the layout at all.
One of the reasons I love open source software is the fact that the community will fix weaknesses is the software. Looking at the codeigniter wiki, I came across the “view object” (http://codeigniter.com/wiki/View_Object/)
The view object is a great solution for adding layouts to the codeigniter framework.
Here is a code sample of how to use the view object in a controller:
$this->load->library('view'); // or autoload
$this->view->layout = 'admin/layout';
$this->view->data(array( // set the view data
'privileges' => $privileges,
'catcode' => -1,
'page' => $page,
));
$this->view->load(array( // load the page partials
'header' => 'header',
'menu' => 'menu',
'content' => 'admin/'.$page,
'footer' => 'footer',
));
$this->view->render(); // create the view or
inside the layout file (admin/layout.php)
<? $header->render(); ?>
<body>
<? $menu->render(); ?>
<div id="mainContent">
<? $content->render(); ?>
</div>
</body>
<? $footer->render(); ?>
You can see that the layout file contains the framework of the page, freeing up the views to be individual pluggable items that can be used across your codeigniter application.
I hope that the codeigniter team takes note of the view object and adds it to the core for codeignier 1.7 or maybe even sooner!