Improving CodeIgniters View Handling

by Josh Highland on April 29, 2008

codeigniter logo Improving CodeIgniters View Handling

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!

  • digg Improving CodeIgniters View Handling
  • facebook Improving CodeIgniters View Handling
  • stumbleupon Improving CodeIgniters View Handling
  • twitter Improving CodeIgniters View Handling
  • delicious Improving CodeIgniters View Handling
  • reddit Improving CodeIgniters View Handling
  • friendfeed Improving CodeIgniters View Handling
  • posterous Improving CodeIgniters View Handling
  • tumblr Improving CodeIgniters View Handling

{ 9 comments… read them below or add one }

1 Alex May 4, 2008 at 7:28 am

Well, you can. Create a view template for the ‘layout’ with all the common data. Add print variables for the different dynamic areas.

Then, load some other views in some temporary variables, instead of echoing them (use the third parameter of load view with a value of “true”).

Use the temporary variables as the data used to display the ‘layout’.

Ex:
$content = $this->load->view(‘content’,$content_data,true);//instead of displaying the view to screen, you get it’s content in $content
$alldata['title'] = “Some page variable”;
$alldata['content_holder'] = $content;//you should have a <?=content_holder?> in your layout
$this->load->view(‘layout’,$alldata);

Sorry if my explanation is not very clear.

2 Josh Highland May 4, 2008 at 4:42 pm

thats interesting, looking at the user guide, http://codeigniter.com/user_guide/general/views.html , the third parameter you mention is not documented. how did you come across it? I wonder why its not in the user guide.

3 Alex May 5, 2008 at 8:21 am

Honestly, I don’t remember. I’ve used CI a while ago, and this way of managing views made more sense to me than the way CakePHP did it (back then, because I haven’t tried it since).

The CI user guide is one of the best, but even so, the user forum adds a lot of good information. If you search there i’m sure you’ll find some interesting pieces of code that are not present in the user guide (actually it makes sense to have a clean user guide that gets you started without adding to much complexity)

4 Josh Highland May 8, 2008 at 8:16 am

Alex, i tried out your approach, and it works great! no need to load extra code or anything.

This is a great discovery. I wonder why it is not documented!

5 John_Betong May 8, 2008 at 8:20 pm

Initially I had similar problems with the HTML block statements.

Now I let CodeIgniter generate “self contained block statement strings” which are used in the following template/view.php

div {border:dotted 0px} /* DEBUG 1px */

<

<

6 John_Betong May 8, 2008 at 9:12 pm

My previous comment did not display correctly so here is the online version which hopefully shows the URL:

johns-jokes.com/downloads/template_joshhighland.txt

7 buzzknow June 16, 2010 at 2:40 am

anyway, how to create simple dynamic page title in code igniter?

thanks

8 Sam IT August 23, 2010 at 11:44 am

While Codeigniter is truly a great framework (which I work with every day) I think it has a lot of other weaknesses that are probably a bit more important that views.

I’m not going to explain too much about those weaknesses, mainly because most of those weaknesses may not be practical for some (if not most) users but just to give you an idea:

* You cannot “extend” the bootstrap process.
* The controller/router/CI_Base are coupled badly, something that for example, prevents from loading multiple controllers.

I have never used the “View Object” class. I think it’s a good idea and that the implementation that be improved a bit.

For example:

When you use the standard CI views and you use an undeclared variable, a notice will be issued by PHP and may or may not be printer to the screen and/or a file.

When you use this class, it seems to me (just by reading the code, I have never used it and hope that I’m correct) that you can easily cause your application to die because you’re calling an undeclared method (rather than a variable).

To make things a bit clear:

* Standard views: < -- will issue a warning/notice;

* "View Object": render() ?> <– will exit;

This could be solved by using PHP’s “magic methods”. __get() and __set() would be a good start.

Also, another quick way is to use the __to_string() magic method so that in the views, you don’t need to call the render() method. You can simply echo the variable. The rest will be done internally.

The only issue (I see) with those suggestions is that they’re PHP-5 specific but in general, I think I’m gonna start using the View Object class.

9 Tahsin Hasan October 4, 2010 at 12:08 am

Hello,

get the advanced layout library on codeigniter on tahsin’s garage.

Leave a Comment

{ 1 trackback }

Previous post:

Next post: