Improving CodeIgniters View Handling

codeigniter logo

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!

LifeStreaming Is Simple As Pie

Its not secret, I love social networking, I cant get enough of it. I also love programming and anything internet related.

I’m not sure how I came across it, but a PHP based, Object Oriented RSS caching tool named SimplePie caught my attention.

Previously I had been using a tool called Last RSS, but I found that Last RSS could not handle ATOM feeds.

Digging into how SimplePie works, I found that I could merge several feeds together, sorted by post time, my first thought was to use this to aggregate and cache all the different music related news feeds that and going to add content to notpopular.com v2.5 (when ever I finish it). I started thinking about it a bit more and thought it would be cool to use SimplePie to mash together all of the different RSS feeds from the different social networks I am on.

It wasn’t that hard to use SimplePie and the various social networks to make something really cool.


//establish the feeds
$pownce = new SimplePie("$pownceRSS_url");
$flickr = new SimplePie("$flickrRSS_url");
$twitter = new SimplePie("$twitterRSS_url");
$digg = new SimplePie("$diggRSS_url");
$youtube = new SimplePie("$youtubeRSS_url");


//merge them all together!
$merged = SimplePie::merge_items(array($pownce, $flickr, $twitter, $digg, $youtube));

Then all you have to do is look over each item in $merged, and output the appropriate info you want.

You can see what I built over at www.JoshHighland.com

One thing I really found great was the API that was on the SimplePie site.

After I put this all together, I found out that there is a term for what I had just built. People are calling them “LifeStreams“, which is a very appropriate term. I thought I had invented something, but guess LifeStreaming is like fire or a spear, given enough time people all over the place will discover it on their own.

I encourage you do set up a LifeStream for yourself and post the URL in the comments below. It’s fun!

My LifeStream: www.JoshHighland.com

UPDATE (10/29/08) :
I was having trouble with my feed from twitter dying after a while. It would only fetch it once, then nothing.I found the answer to my problem on the simplePie blog, http://simplepie.org/blog/2008/08/16/twitter-bug/. I followed the instructions and commented out the If statement on lines 1583-1586, and the twitter feed started to work again. I hope that twitter updates their RSS service so hacks like this aren’t needed.

WordPress 2.5 image upload problem : SOLVED

This afternoon I upgraded my blog to the latest version of wordpress, 2.5. The install went great and it was simple to do. I didn’t realize that I had any problems until I went to make a blog post and upload an image. When I tried to upload, I was greeted with the following:

Something was for sure messed it. I tried it in several browsers, but had the same results. no luck. I started to go some searching online but since wordpress 2.5 is so new, there was little info to be found. After about an hour of stumbeling around I came across a post on a forum where someone said they got the image upload to work by adjusting their .htaccess file on thier site with the following code.

<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>

I made the change to my .htaccess file, and like magic the image upload feature started to work again!

I am making this post in the hopes that other people with similar problems will stumble across this post and be able to fix their problem with out having to wade through all the pages of people trying to diagnose what the problem is when the solution is super easy.

The Windows tools that I use

windows_tools.jpg

I am not a huge fan of windows, but it the operating system I have been running for the past 14 years (if you dont count college, or my current laptop… both were/are linux based). I have Windows Vista at home, and I have to use Windows XP at work.

Over the years, I have found programs that have worked for me. They have worked well, and helped me to get things done. I decided to make a list of the applications that I use on a rather regular basis. These are the things that I would for sure reinstall if my computer was to die tonight. Most of the software is free, and open source. As a result, many of the programs I am about to list also are available for Linux and Apple OSX. So here is the list of windows program that I love, in no particular order (expect Firefox, its at the top of the list)

Web Browsing:

File Searching:

File Comparison:

Text Editor:

Sound Editing:

Windows Enhancements:

Instant Messaging:

CD Burning:

Anti-virus:

Spyware:

Image sharing/hosting:

VOIP:

P2P:

Backup:

  • carbonite (http://www.carbonite.com)
    $50 a year for unlimited off site backup? you better believe i use this. you should also!

Blogging:

Programming:

SSH:

RSS Reader:

FTP:

Key Logging: