View Helpers
Posted in Solar on June 6th, 2009 by Jon – Be the first to commentView helpers are very useful for performing repetitive tasks and, more importantly, an essential way to keep domain logic out of your views. View helpers can be very simple, such as displaying a date in a particular format, or they can be quite complex, such as displaying a complete html form. Solar is packed with many very useful view helpers. Additionally, the way Solar applications are organized makes it easy to create your own view helpers and have them available to several applications.
To use a view helper, simply call it in your view. Take, for example, the title view helper bundled with Solar. This helper simply puts the <title> tags around whatever text you pass to it.
<html> <head> <?php // assume $pagetitle was set in the controller and is public ?> <?php echo $this->title($this->pagetitle); ?> </head>
Ok, so that is a very simple example that you might never use, but you never know. What is important in a helper like this is the automatic escaping of text.
One view helper that I use all the time is the action view helper (Solar_View_Helper_Action). This helper displays an escaped link with the properly formed <a> tags. It takes 3 parameters, the second and third are optional.
It works something like this:
<?php echo $this->action($spec, $text = null, $attribs = null); ?>
$spec is the link,
$text is the text (or a locale key for automatic translation),
$attribs is an array of option attributes to use in the link, such as class, style, or even onclick.
For example:
<?php // assume TEXT_BROWSE is a locale key and translates to 'Browse' ?> <?php echo $this->action('index/browse', 'TEXT_BROWSE'); ?>
This will output:
<a href="/docroot/index/browse">Browse</a>
The docroot part comes from the uri action path in your config file: $config['Solar_Uri_Action']['path'] = ‘/docroot’;
What’s interesting here is that, if you dive into the code, the action helper actually refers to other helpers, actionHref and escape via the $this->_view property. Now you can see how powerful view helpers can be! In fact, many of Solar’s view helpers utilize other Solar view helpers. It just makes sense.
Writing your own view helpers is a snap. You can make them as simple or as complex as you like. Each time you create an application using Solar’s Command Line Interface (CLI), a Helper sub folder is creating in your application’s namespace. For example, if your vendor is named Vendor and your application is named Example, then the Helper folder will exist in a folder structure similar to this.
Vendor/
App/
Example/
Helper/
Layout/
Locale/
View/
Example.php
So, here is an example of a very simple view helper that will take a Canadian postal code in this format A1B2C3 and convert it to the proper A1B 2C3 format.
The helper file is named PostalCode.php and is located in the Helper sub folder of your application.
Here is what it might look like:
<?php /** * View Helper to format a postal code from 6 alpha numeric * characters without a space * to 6 with a space. T9E5W7->T9E 5W7 */ class Vendor_App_Example_Helper_PostalCode extends Solar_View_Helper { public function postalCode($code) { // Get the first 3 chars (note the use of another // view helper called escape) $first_three = $this->_view->escape(strtoupper(substr($code, 0, 3))); // Get the last 3 chars $last_three = $this->_view->escape(strtoupper(substr($code, 3, 3))); // Return the newly formated code with a space return $first_three." ".$last_three; } } ?>
To use this view helper in your view it’s as simple as:
<?php echo $this->postalCode($code); ?>
Another nice feature of view helpers is that they ultimately extend Solar_Base. This means they handle the config files just like all the other Solar classes. If you don’t know about Solar’s config file, then you should definitely read about it.
If you want to create a view helper that has a property that can be customized, then just follow Solar’s standards for classes and objects. Here is an example taken from Solar’s date view helper. I have simplified this from the real file.
<?php class Solar_View_Helper_Date extends Solar_View_Helper { /** * * User-defined configuration values. * */ protected $_Solar_View_Helper_Date = array( 'format' => 'Y-m-d', ); public function __construct($config = null) { parent::__construct($config); } /** * * Outputs a formatted date. * */ public function date($spec, $format = null) { if (! $format) { $format = $this->_config['format']; } return date($format, strtotime($spec)); } } ?>
As you can see, the default format is ‘Y-m-d’. But, that can be changed in 2 ways.
First, you can pass whatever format you want to the helper, and second, you can set a $config value in your config file to override the default.
In a view it would look like this:
<?php echo $this->date('Yesterday', 'm jS, Y'); ?>
If you changed the default in your config file, the file would have an entry like this:
<?php $config['Solar_View_Helper_Date']['format'] = 'm jS, Y'; ?>
That is the basic idea behind view helpers. Remember, if you need to access the current view object from within the view helper, you do so using $this->_view.
What’s really important is that these keep unnecessary logic out of the view!