Improving CodeIgniters View Handling

Uncategorized 6 Comments »

codeigniter logo

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!

Better Late Then Never!

Uncategorized No Comments »

a lot has happened since I have last posted on my blog… Im sure im missing some things

  • The United States has a new president
  • I have started work on a new lyrics database for notpop
  • Thommy and Krissi are dating and Im stoaked for them
  • I found out some cool things from Yenny
  • I have developed a mean cough that keeps me up most the night
  • I build a computer for hardcore Steve, which quickly caught a virus, so now i have to fix it again.
  • I have been super busy at work. Busy to the point where I dont have time to post on my blog
  • I updated the xdeathstarx website with a new image gallery
  • I made notpop a bit more cross browser compliant (something i have be meaning to do for a year now)
  • I-rediscovered the joys of bit torrents
  • Jason Keller has been staying at my house for the last week
  • I scraped the acoustic popcorn style ceiling down in my downstairs
  • The dishwasher broke, I need to fix it/get a new one
  • I have realized how financially straining having a house is
  • I talked to a tile worker who is going to install tile in my upstairs bathroom.
  • I need to make another run to ikea for an organizer for my computer room
  • I ordered a lettermen jacket online
  • I ordered roman shades for my bedroom
  • I ordered closet doors for the bedrooms in my house
  • Amys sister and Ed decided to move up their wedding date to this coming saturday, so I have to drive to Vegas on saturday
  • Friday night we are playing the glasshouse with NIV. Im nervous. I hate shows with barricades. Reminds me of the Atreyu tour. The crowd seems so far away.
  • I talked to Sasha last night. I miss her and worry about her.
  • I got this crazy tribute to AFI done by this string quartet… wild stuff
  • I started playing starcraft again. Jason keller and I spend way to much time playing it. 4 Hours a night is not healthy, is it?
  • Im trying to book a show for my wedding at showcase. Bleeding through @ glasshouse isnt going to happen :( to bad. Showcase is like home anyways. Anyone know a big drawing, headlining band that wants to play in late jan/early feb?
  • Young Matt is now a notPop board admin. He wants me to create a record pressing database… I think its a damn good idea. I'll just add that to my list of 100 websites that I need to create.
  • We started to have church at the Redlands Bowl
  • I told Keller he could live with me for a while
WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in