Prev | Current Page 57 | Next

Rob Allen, Nick Lo, and Steven Brown

"Zend Framework in Action"


View Helper Functions
Zend_View contains a number of helpful functions to make writing templates easier and allows for you to
write your own. These are functions are known as View Helpers and exist in their own directory. As we have
already seen, the most common view helper is the escape() function which is built into the Zend_View class
itself. Every other helper exists in its own class and is automatically loaded by Zend_View. Let??™s create a
simple formatting helper for displaying a cash amount. Consider that we need to display a monetary value that
may be negative. In the UK, for a value of 10, the display would be ??10.00 and for a value of -10, then the
display would be -??10.00.
We would use this helper in our templates like this:

He gave me formatCurrency(10);?>.


Which outputs the correctly formatted amount as shown in Figure 2.5.
Figure 2.5: Front controller plug-in correctly routing to the IndexController??™s noRoute action.
To separate our helpers from the default ones, we will use the class prefix ZFiA_View_Helper and so our
helper class is called ZFiA_View_Helper_FormatCurrency as shown in listing 2.6.
Listing 2.6: ZFiA_Helper_ FormatCurrency view helper
class ZFiA_View_Helper_FormatCurrency {
public function formatCurrency($value, $symbol='£') #1
{
$output = $value;
$value = trim($value);
if (is_numeric($value)) { #2
Licensed to Menshu You

Pages:
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
prezenty ATC mieszkania szczecin A*Teens nowoczesne meble