Ever since getting on the rapid development framework bandwagon, the one part I’ve not been totally sold on is the use of helpers, specifically, helpers which replace HTML code.
The reasoning is that I’ve never really seen the point of them. To give an example, CakePHP has an HTML helper for creating links - and the code looks a little something like this:
<?php echo $html->link('link text description', '/path/to/link') ?>
Which isn’t really much more efficient than it’s HTML counterpart, written like this:
<a href="url">link text description</a>
In fact, the HTML is shorter to write out. So why even bother with HTML helpers?
I got into the habit of using them, mostly because when I got into the CakePHP framework, it just seemed like the thing to do. It made me feel all Cake-y, and yes, it does come in handy when you are using php variables in the link creation. However, it became abundantly clear to me the other day that it can really cripple regular website maintenance for developers who come into the project not knowing Cake.
One of my clients, who I had created a site for using Cake, needed to change a link in one of the page views, saw the $html->link() function, didn’t understand it, and thought that all links had to be created in this special way. Which is totally understandable - the client doesn’t know a lot about the Cake framework (or frameworks in general), and wasn’t aware that all the link function does is parse out an HTML link (and that he could have just used a normal HTML link and it would have been the same).
So, the question remains - are HTML helpers worth it?
That answer is easy! Of course they are handy! Once you use one of those helpers, you know you can always extend that functionality if need be.
For instance, let’s say you end up using the
$html->image. That by default uses a path relative towebroot/images. Perfect, but what if you want to serve those images off another server? You can modify the helper to pull from http://assets.yourdomain.com/images without touching all/any of your images.Now can I think of an example for
$html->link? no. But you never know.Yes they are quite useful and can offer the developer a lot more support later on. Lets say for example you had 1,000 links pointing to external sites throughout your site and you wanted to change it so they opened in a new window. If they were standard HTML links you would have to go through each one and edit it individually. Using this ‘helper’ method means that within the function that handles the link creation you could easily change it to affect all links.
You could also add conditionals in the function to determine what page the link is being called on so you can set differences in links depending on what page they are on.
Just my two cents
- Michael
Thanks for your comments, guys. Those are very good points and make me feel a lot more confident in continuing to use Helpers.