Posts Tagged ‘helper’

View Helpers

Posted in Solar on June 6th, 2009 by Jon – Be the first to comment

View 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."&nbsp;".$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!

A real Solar application

Posted in Solar on April 24th, 2009 by Jon – 2 Comments

I wrote a web-based application for my dad recently using, you guessed it, Solar. He had been using a really old version of Quattro Pro for keeping track of all his receivables, invoices, etc. He finally got a new computer, which was 64-bit and Vista, and he couldn’t even load Quattro Pro onto it, so it was time to find a new solution. This, IMHO, was a bit of a blessing. (Holy crap! I haven’t done my taxes yet!!!). Anyway, I offered to create a database and a web-based application that would sever existing ties between the data and presentation. He’s pretty clever, so that seemed like a great idea to him.

So I created the DB, which consisted of about 8 tables and 3 views. This isn’t rocket science, but it’s a great case for a relational database. Next, I whipped up a Solar application to keep track of clients, invoices, products, receivables, etc. It’s not fancy, but it works pretty well for a version 1. One of the coolest things about it – thanks to Solar, I didn’t write a single SQL query! The Solar models worked so well for me, that with a single fetch, I can get every bit of data I need, quickly and efficiently. They also make it dead simple to create or update data, complete with Super Awesome (yes, that’s capitalized and bold) form validation.

The next steps for the application will be to make it more user-friendly. It will be pretty easy to sweeten it up using jQuery UI and custom Solar view helpers. Maybe even a touch of Ajax.

Solar definitely makes my job easier.

Oh, and one of the best things about the application – it has the Solar Powered logo on it :)

Views, Layouts, and Locales

Posted in Solar on April 8th, 2009 by Jon – 1 Comment

Views and layouts are responsible for displaying content in the browser. Views are generally specific to a controller action. For example, an action method such as actionIndex() would display its output in a view called index.php. Layouts, on the other hand, are typically shared by the entire application and wrap around the view. A layout usually contains the header, footer, navigation, and the content area where the view’s output is displayed.
read more »