skip to content

DerekAllard.com : CodeIgniter, ExpressionEngine, and the World of Web Design

Giving your Helpers a little Help

August 12th, 2007

CodeIgniter is one of the most flexible, powerful, unobtrusive frameworks you could choose to use. It’s so useful in many ways, including pre-existing convenience libraries (I can’t live without the email library anymore), plugins (for handy little tools such as javascript calendars and CAPTCHA creation) and the appropriately named “helpers”.

In the world of CodeIgniter libraries, plugins and helpers, the libraries are Rock Stars. They get most of your attention, they do most of the heavy lifting. They date super-models, party in Europe, and return on Monday to keep your application humming along (sometimes Tuesday… you know those darned unreliable rock stars). They can be extended, overwritten, and you can create your own, wholly new libraries just for your application. Wow. No wonder super-models want to date them.

One of the most handy aspects of libraries, is that you can create a libraries folder in system/application/libraries, and leave the existing CodeIgniter libraries untouched. This makes upgrading to newer versions of CI a breeze, as you never need to worry about going in and re-implementing all the changes you made to a library.  From the userguide:

As an added bonus, CodeIgniter permits your libraries to extend native classes if you simply need to add some functionality to an existing library. Or you can even replace native libraries just by placing identically named versions in your application/libraries folder.

And while libraries are doing all this work, we’re left with the humble, but infinitely handy helper. Here’s what the userguide has to say about helpers.

Helpers, as the name suggests, help you with tasks. Each helper file is simply a collection of functions in a particular category.

Makes them sound like one-trick ponies; but they are not. Helpers, while not quite as flexible as libraries, are tremendously useful. They are written procedurally, instead of object oriented (as the rest of CodeIgniter is). And while they can’t be extended as libraries can, a little known fact is that they can be overwritten. Better yet, is the method in which you’d override them. Much like libraries, the trick lies in creating a folder underneath system/application for helpers. Then, when CodeIgniter loads helpers, it looks first in your application/helpers folder, and only if that folder can’t be found does it then fall back on the default system/helpers folder. So you can add your own helpers, or overwrite existing ones.

Here’s a case study. In BambooInvoice I use the date helper’s timespan() function to determine exactly how overdue an invoice might be. My problem is that the default timespan() function keeps tabs down to the second of the span of time. When tracking invoices, I don’t care how many hours, minutes and seconds have passed, only how many days and weeks (or months if you have a really bad-paying client ;)) So for use in Bamboo, I want that helpers default output changed. The solution, to remove or comment out those lines of the helper.

But the problem with modifying the system helper is that in the next upgrade, that helper gets overwritten again. Since I use BambooInvoice as part of my testing codebase for vetting new CodeIgniter code, I tend to overwrite these files a lot. This then required carefully keeping track of what helpers were modified, and introduced some other problems. A preferable approach would be to simply make a copy of system/helpers/date_helper.php in application/helpers. Now, the application helper will be loaded preferentially, and I can modify to my hearts content without ever fearing I’ll accidentally overwrite my changes.

Application based helpers are a handy feature indeed. There have been discussions about changing the procedural structure of helpers to allow for extending and over-riding, but for now, the simplicity of helper functions are preserved, and you can easily maintain your own modified versions.

How are you using CodeIgniter helpers? If you’ve modified any of the base helpers, or created your own, I’d love to hear about it here.

This entry was made on August 12th, 2007 @ 18:30 and filed into , .

Comments

Jon wrote on August 12th, 2007 @ 21:30

I have a helper I call the “header helper”.  Sometimes, I use the same html layout view over and over again - but I don’t want to include every bit of javascript/css/script on every page.

To reduce page weight, I devised the “header helper” to write out some of these things for me.

For example, let’s say I have a bunch of CSS files that I need to load - but they are in different directories (I keep them in different places depending on if they are “library” CSS like YUI or my own CSS):  I would use my header_helper like this:
controller:

<?
$css 
= array(
  
"/public/css"=>array("file1.css","file2.css"),
"/public/libraries/css"=>arrray("lib1.css","lib2.css")
);
?>

In the view:

<?show_css($css?>

Which would output something like:
<link rel="stylesheet" type="text/css" href="public/css/file1.css" />
...file2, file3
<link rel="stylesheet" type="text/css" href="/public/libraries/css/lib1.css" />
...lib2, lib3, etc

You get the idea.  The same goes for external JS files.  I can also write blocks of scripts (as needed) and use them as views:

$looseScript $this->load->view("looseScript1",$data,true);

In the view:

<head>

<?$looseScript ?>

Jim O'Halloran wrote on August 13th, 2007 @ 0:17

I’ve got a few helpers I’ve written for various projects, and usually reuse in new projects.

The first is the au_date helper, which has a some functions for formatting unix timestamps according to local (Australian) conventions and parsing aussie formatted dates back into Unix timestamps.

I also have a validation helper.  I often find myself using a form which needs to be preloaded with data, or if it failed validation needs to contain the data from the last submit.  I’ve got a helper with a few functions similar to CI’s own set_* functions which ease this awkward process a little.

I’ve also got a Credit Card helper which contains various functions for cleaming up credit card numbers, truncating them, validating expiry dates, and performing mod10 validation on the number itself.

Helper’s are certainly helpful little things!

Jim.

Derek wrote on August 13th, 2007 @ 15:10

@Jon: I like this idea a lot.  There have been many good efforts to integrate views and assets more easily.  I like your solution.

@Jim: These sound fantastic!  Would you be willing to post them on the CodeIgniter wiki?

Jim O'Halloran wrote on August 14th, 2007 @ 2:42

I’m happy to post the au_date helper and the Credit Card helper to the Wiki. 

I’I’m not particularly happy with the validation helper at the moment, it helps, but I’m wondering if a modified Validation library might be a better solution.

Jim.

Fratier wrote on August 22nd, 2007 @ 13:43

I entered your site with exactly the same problem with the date helper, but I just figured out that it can be solved creating arrays of each element (moth, day, etc...), then you just put some “filters” so you decide which one to show. I hope it helps, this way you don’t have to modify the core system.

Kivutar wrote on August 26th, 2007 @ 14:48

I recently coded a bbCode helper that looks like to the original smiley helper.

It is documented in the CodeIgniter wiki at that page.

I’m also working on a preview helper, or maybe a plugin… that would generate a javascript function for live preview of smileys and bbcode.

Jim O'Halloran wrote on August 27th, 2007 @ 1:20

Hi there!

Just thought I’d drop in another comment to let everyone know that my Credit Card and Australian Date helpers have now been posted to the CI Wiki.

Hope someone else finds them as useful as I do.

Jim.

Post a Comment

Sorry, comments are automatically closed after 45 days, or sooner if one entry gets targetted by spammers. Why not contact me directly?