1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.view.helpers" xmlns:xi="http://www.w3.org/2001/XInclude">
4 <title>View Helpers</title>
7 In your view scripts, often it is necessary to perform certain
8 complex functions over and over: e.g., formatting a date,
9 generating form elements, or displaying action links. You can
10 use helper classes to perform these behaviors for you.
14 A helper is simply a class. Let's say we want a helper named 'fooBar'.
15 By default, the class is prefixed with 'Zend_View_Helper_'
16 (you can specify a custom prefix when setting a helper path), and the
17 last segment of the class name is the helper name; this segment should
18 be TitleCapped; the full class name is then:
19 <classname>Zend_View_Helper_FooBar</classname>. This class should contain at the
20 minimum a single method, named after the helper, and camelCased:
21 <methodname>fooBar()</methodname>.
25 <title>Watch the Case</title>
28 Helper names are always camelCased, i.e., they never begin with an
29 uppercase character. The class name itself is MixedCased, but the
30 method that is actually executed is camelCased.
35 <title>Default Helper Path</title>
38 The default helper path always points to the Zend Framework view
39 helpers, i.e., 'Zend/View/Helper/'. Even if you call
40 <methodname>setHelperPath()</methodname> to overwrite the existing paths, this
41 path will be set to ensure the default helpers work.
46 To use a helper in your view script, call it using
47 <command>$this->helperName()</command>. Behind the scenes,
48 <classname>Zend_View</classname> will load the
49 <classname>Zend_View_Helper_HelperName</classname> class, create an object
50 instance of it, and call its <methodname>helperName()</methodname> method. The
51 object instance is persistent within the <classname>Zend_View</classname>
52 instance, and is reused for all future calls to
53 <command>$this->helperName()</command>.
56 <sect2 id="zend.view.helpers.initial">
57 <title>Initial Helpers</title>
60 <classname>Zend_View</classname> comes with an initial set of helper classes,
61 most of which relate to form element generation and perform
62 the appropriate output escaping automatically. In addition, there
63 are helpers for creating route-based <acronym>URL</acronym>s and <acronym>HTML</acronym>
64 lists, as well as declaring variables. The currently shipped helpers include:
70 <methodname>declareVars()</methodname>: Primarily for use when using
71 <methodname>strictVars()</methodname>, this helper can be used to declare
72 template variables that may or may not already be set in the
73 view object, as well as to set default values. Arrays passed as
74 arguments to the method will be used to set default values;
75 otherwise, if the variable does not exist, it is set to an empty string.
81 <methodname>fieldset($name, $content, $attribs)</methodname>: Creates an
82 <acronym>XHTML</acronym> fieldset. If <varname>$attribs</varname> contains a
83 'legend' key, that value will be used for the fieldset legend. The
84 fieldset will surround the <varname>$content</varname> as provided to
91 <methodname>form($name, $attribs, $content)</methodname>: Generates an
92 <acronym>XHTML</acronym> form. All <varname>$attribs</varname> are escaped and
93 rendered as <acronym>XHTML</acronym> attributes of the form tag. If
94 <varname>$content</varname> is present and not a boolean
95 <constant>FALSE</constant>, then that content is rendered within the start and
96 close form tags; if <varname>$content</varname> is a boolean
97 <constant>FALSE</constant> (the default), only the opening form tag is
104 <methodname>formButton($name, $value, $attribs)</methodname>: Creates an
105 <button /> element.
111 <methodname>formCheckbox($name, $value, $attribs, $options)</methodname>:
112 Creates an <input type="checkbox" /> element.
116 By default, when no $value is provided and no $options are
117 present, '0' is assumed to be the unchecked value, and '1'
118 the checked value. If a $value is passed, but no $options
119 are present, the checked value is assumed to be the value
124 $options should be an array. If the array is indexed, the
125 first value is the checked value, and the second the
126 unchecked value; all other values are ignored. You may also
127 pass an associative array with the keys 'checked' and
132 If $options has been passed, if $value matches the checked
133 value, then the element will be marked as checked. You may
134 also mark the element as checked or unchecked by passing a
135 boolean value for the attribute 'checked'.
139 The above is probably best summed up with some examples:
142 <programlisting language="php"><![CDATA[
143 // '1' and '0' as checked/unchecked options; not checked
144 echo $this->formCheckbox('foo');
146 // '1' and '0' as checked/unchecked options; checked
147 echo $this->formCheckbox('foo', null, array('checked' => true));
149 // 'bar' and '0' as checked/unchecked options; not checked
150 echo $this->formCheckbox('foo', 'bar');
152 // 'bar' and '0' as checked/unchecked options; checked
153 echo $this->formCheckbox('foo', 'bar', array('checked' => true));
155 // 'bar' and 'baz' as checked/unchecked options; unchecked
156 echo $this->formCheckbox('foo', null, null, array('bar', 'baz'));
158 // 'bar' and 'baz' as checked/unchecked options; unchecked
159 echo $this->formCheckbox('foo', null, null, array(
164 // 'bar' and 'baz' as checked/unchecked options; checked
165 echo $this->formCheckbox('foo', 'bar', null, array('bar', 'baz'));
166 echo $this->formCheckbox('foo',
168 array('checked' => true),
169 array('bar', 'baz'));
171 // 'bar' and 'baz' as checked/unchecked options; unchecked
172 echo $this->formCheckbox('foo', 'baz', null, array('bar', 'baz'));
173 echo $this->formCheckbox('foo',
175 array('checked' => false),
176 array('bar', 'baz'));
180 In all cases, the markup prepends a hidden element with the
181 unchecked value; this way, if the value is unchecked, you
182 will still get a valid value returned to your form.
188 <methodname>formErrors($errors, $options)</methodname>: Generates an
189 <acronym>XHTML</acronym> unordered list to show errors.
190 <varname>$errors</varname> should be a string or an array of strings;
191 <varname>$options</varname> should be any attributes you want
192 placed in the opening list tag.
196 You can specify alternate opening, closing, and separator
197 content when rendering the errors by calling several methods
204 <methodname>setElementStart($string)</methodname>; default is
205 '<ul class="errors"%s"><li>', where %s
206 is replaced with the attributes as specified in
207 <varname>$options</varname>.
213 <methodname>setElementSeparator($string)</methodname>; default
214 is '</li><li>'.
220 <methodname>setElementEnd($string)</methodname>; default is
221 '</li></ul>'.
229 <methodname>formFile($name, $attribs)</methodname>: Creates an
230 <input type="file" /> element.
236 <methodname>formHidden($name, $value, $attribs)</methodname>: Creates an
237 <input type="hidden" /> element.
243 <methodname>formLabel($name, $value, $attribs)</methodname>: Creates a
244 <label> element, setting the <property>for</property> attribute to
245 <varname>$name</varname>, and the actual label text to
246 <varname>$value</varname>. If <emphasis>disable</emphasis> is passed in
247 <property>attribs</property>, nothing will be returned.
253 <methodname>formMultiCheckbox($name, $value, $attribs, $options,
254 $listsep)</methodname>: Creates a list of checkboxes.
255 <varname>$options</varname> should be an associative array, and may be
256 arbitrarily deep. <varname>$value</varname> may be a single value or
257 an array of selected values that match the keys in the
258 <varname>$options</varname> array. <varname>$listsep</varname> is an
259 <acronym>HTML</acronym> break ("<br />") by default. By default, this
260 element is treated as an array; all checkboxes share the same name, and are
261 submitted as an array.
267 <methodname>formPassword($name, $value, $attribs)</methodname>: Creates an
268 <input type="password" /> element.
274 <methodname>formRadio($name, $value, $attribs, $options)</methodname>:
275 Creates a series of <input type="radio" /> elements, one
276 for each of the $options elements. In the $options array, the
277 element key is the radio value, and the element value is the
278 radio label. The $value radio will be preselected for you.
284 <methodname>formReset($name, $value, $attribs)</methodname>: Creates an
285 <input type="reset" /> element.
291 <methodname>formSelect($name, $value, $attribs, $options)</methodname>:
292 Creates a <select>...</select> block, with one
293 <option>one for each of the $options elements. In the
294 $options array, the element key is the option value, and the
295 element value is the option label. The $value option(s) will be
302 <methodname>formSubmit($name, $value, $attribs)</methodname>: Creates an
303 <input type="submit" /> element.
309 <methodname>formText($name, $value, $attribs)</methodname>: Creates an
310 <input type="text" /> element.
316 <methodname>formTextarea($name, $value, $attribs)</methodname>: Creates a
317 <textarea>...</textarea> block.
323 <methodname>url($urlOptions, $name, $reset)</methodname>: Creates a
324 <acronym>URL</acronym> string based on a named route.
325 <varname>$urlOptions</varname> should be an associative array of key/value pairs
326 used by the particular route.
332 <methodname>htmlList($items, $ordered, $attribs, $escape)</methodname>:
333 generates unordered and ordered lists based on the <varname>$items</varname>
334 passed to it. If <varname>$items</varname> is a multidimensional
335 array, a nested list will be built. If the <varname>$escape</varname>
336 flag is <constant>TRUE</constant> (default), individual items will be escaped
337 using the view objects registered escaping mechanisms; pass a
338 <constant>FALSE</constant> value if you want to allow markup in your lists.
344 Using these in your view scripts is very easy, here is an example.
345 Note that you all you need to do is call them; they will load
346 and instantiate themselves as they are needed.
349 <programlisting language="php"><![CDATA[
350 // inside your view script, $this refers to the Zend_View instance.
352 // say that you have already assigned a series of select options under
353 // the name $countries as array('us' => 'United States', 'il' =>
354 // 'Israel', 'de' => 'Germany').
356 <form action="action.php" method="post">
357 <p><label>Your Email:
358 <?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
360 <p><label>Your Country:
361 <?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
363 <p><label>Would you like to opt in?
364 <?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no')) ?>
370 The resulting output from the view script will look something like this:
373 <programlisting language="php"><![CDATA[
374 <form action="action.php" method="post">
375 <p><label>Your Email:
376 <input type="text" name="email" value="you@example.com" size="32" />
378 <p><label>Your Country:
379 <select name="country">
380 <option value="us" selected="selected">United States</option>
381 <option value="il">Israel</option>
382 <option value="de">Germany</option>
385 <p><label>Would you like to opt in?
386 <input type="hidden" name="opt_in" value="no" />
387 <input type="checkbox" name="opt_in" value="yes" checked="checked" />
392 <xi:include href="Zend_View-Helpers-Action.xml" />
393 <xi:include href="Zend_View-Helpers-BaseUrl.xml" />
394 <xi:include href="Zend_View-Helpers-Currency.xml" />
395 <xi:include href="Zend_View-Helpers-Cycle.xml" />
396 <xi:include href="Zend_View-Helpers-Partial.xml" />
397 <xi:include href="Zend_View-Helpers-Placeholder.xml" />
398 <xi:include href="Zend_View-Helpers-Doctype.xml" />
399 <xi:include href="Zend_View-Helpers-HeadLink.xml" />
400 <xi:include href="Zend_View-Helpers-HeadMeta.xml" />
401 <xi:include href="Zend_View-Helpers-HeadScript.xml" />
402 <xi:include href="Zend_View-Helpers-HeadStyle.xml" />
403 <xi:include href="Zend_View-Helpers-HeadTitle.xml" />
404 <xi:include href="Zend_View-Helpers-HtmlObject.xml" />
405 <xi:include href="Zend_View-Helpers-InlineScript.xml" />
406 <xi:include href="Zend_View-Helpers-Json.xml" />
407 <xi:include href="Zend_View-Helpers-Navigation.xml" />
408 <xi:include href="Zend_View-Helpers-Translate.xml" />
411 <sect2 id="zend.view.helpers.paths">
412 <title>Helper Paths</title>
415 As with view scripts, your controller can specify a stack of paths
416 for <classname>Zend_View</classname> to search for helper classes. By default,
417 <classname>Zend_View</classname> looks in "Zend/View/Helper/*" for helper
418 classes. You can tell <classname>Zend_View</classname> to look in other
419 locations using the <methodname>setHelperPath()</methodname> and
420 <methodname>addHelperPath()</methodname> methods. Additionally, you can
421 indicate a class prefix to use for helpers in the path provided, to
422 allow namespacing your helper classes. By default, if no class
423 prefix is provided, 'Zend_View_Helper_' is assumed.
426 <programlisting language="php"><![CDATA[
427 $view = new Zend_View();
429 // Set path to /path/to/more/helpers, with prefix 'My_View_Helper'
430 $view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');
434 In fact, you can "stack" paths using the
435 <methodname>addHelperPath()</methodname> method. As you add paths to the stack,
436 <classname>Zend_View</classname> will look at the most-recently-added path for
437 the requested helper class. This allows you to add to (or even
438 override) the initial distribution of helpers with your own custom
442 <programlisting language="php"><![CDATA[
443 $view = new Zend_View();
444 // Add /path/to/some/helpers with class prefix 'My_View_Helper'
445 $view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
446 // Add /other/path/to/helpers with class prefix 'Your_View_Helper'
447 $view->addHelperPath('/other/path/to/helpers', 'Your_View_Helper');
449 // now when you call $this->helperName(), Zend_View will look first for
450 // "/path/to/some/helpers/HelperName" using class name
451 // "Your_View_Helper_HelperName", then for
452 // "/other/path/to/helpers/HelperName.php" using class name
453 // "My_View_Helper_HelperName", and finally for
454 // "Zend/View/Helper/HelperName.php" using class name
455 // "Zend_View_Helper_HelperName".
459 <sect2 id="zend.view.helpers.custom">
460 <title>Writing Custom Helpers</title>
463 Writing custom helpers is easy; just follow these rules:
469 While not strictly necessary, we recommend either implementing
470 <classname>Zend_View_Helper_Interface</classname> or extending
471 <classname>Zend_View_Helper_Abstract</classname> when creating your
472 helpers. Introduced in 1.6.0, these simply define a
473 <methodname>setView()</methodname> method; however, in upcoming releases, we
474 plan to implement a strategy pattern that will simplify much of
475 the naming schema detailed below. Building off these now will
476 help you future-proof your code.
482 The class name must, at the very minimum, end with the helper
483 name itself, using MixedCaps. E.g., if you were writing a
484 helper called "specialPurpose", the class name would minimally
485 need to be "SpecialPurpose". You may, and should, give the class
486 name a prefix, and it is recommended that you use 'View_Helper'
487 as part of that prefix: "My_View_Helper_SpecialPurpose". (You
488 will need to pass in the prefix, with or without the trailing
489 underscore, to <methodname>addHelperPath()</methodname> or
490 <methodname>setHelperPath()</methodname>).
496 The class must have a public method that matches the
497 helper name; this is the method that will be called when
498 your template calls "$this->specialPurpose()". In our
499 "specialPurpose" helper example, the required method
500 declaration would be "public function specialPurpose()".
506 In general, the class should not echo or print or otherwise
507 generate output. Instead, it should return values to be
508 printed or echoed. The returned values should be escaped
515 The class must be in a file named after the helper class. Again
516 using our "specialPurpose" helper example, the file has to be
517 named "SpecialPurpose.php".
523 Place the helper class file somewhere in your helper path stack, and
524 <classname>Zend_View</classname> will automatically load, instantiate,
525 persist, and execute it for you.
529 Here is an example of our <classname>SpecialPurpose</classname> helper code:
532 <programlisting language="php"><![CDATA[
533 class My_View_Helper_SpecialPurpose extends Zend_View_Helper_Abstract
535 protected $_count = 0;
536 public function specialPurpose()
539 $output = "I have seen 'The Jerk' {$this->_count} time(s).";
540 return htmlspecialchars($output);
546 Then in a view script, you can call the <classname>SpecialPurpose</classname>
547 helper as many times as you like; it will be instantiated once, and
548 then it persists for the life of that <classname>Zend_View</classname>
552 <programlisting language="php"><![CDATA[
553 // remember, in a view script, $this refers to the Zend_View instance.
554 echo $this->specialPurpose();
555 echo $this->specialPurpose();
556 echo $this->specialPurpose();
560 The output would look something like this:
563 <programlisting language="php"><![CDATA[
564 I have seen 'The Jerk' 1 time(s).
565 I have seen 'The Jerk' 2 time(s).
566 I have seen 'The Jerk' 3 time(s).
570 Sometimes you will need access to the calling <classname>Zend_View</classname>
571 object -- for instance, if you need to use the registered encoding,
572 or want to render another view script as part of your helper. To get
573 access to the view object, your helper class should have a
574 <methodname>setView($view)</methodname> method, like the following:
577 <programlisting language="php"><![CDATA[
578 class My_View_Helper_ScriptPath
582 public function setView(Zend_View_Interface $view)
587 public function scriptPath($script)
589 return $this->view->getScriptPath($script);
595 If your helper class has a <methodname>setView()</methodname> method, it will be
596 called when the helper class is first instantiated, and passed the
597 current view object. It is up to you to persist the object in your
598 class, as well as determine how it should be accessed.
602 If you are extending <classname>Zend_View_Helper_Abstract</classname>, you do
603 not need to define this method, as it is defined for you.
607 <sect2 id="zend.view.helpers.registering-concrete">
608 <title>Registering Concrete Helpers</title>
611 Sometimes it is convenient to instantiate a view helper, and then register it with the
612 view. As of version 1.10.0, this is now possible using the
613 <methodname>registerHelper()</methodname> method, which expects two arguments: the
614 helper object, and the name by which it will be registered.
617 <programlisting language="php"><![CDATA[
618 $helper = new My_Helper_Foo();
619 // ...do some configuration or dependency injection...
621 $view->registerHelper($helper, 'foo');
625 If the helper has a <methodname>setView()</methodname> method, the view object will call
626 this and inject itself into the helper on registration.
630 <title>Helper name should match a method</title>
633 The second argument to <methodname>registerHelper()</methodname> is the name of the
634 helper. A corresponding method name should exist in the helper; otherwise,
635 <classname>Zend_View</classname> will call a non-existent method when invoking the
636 helper, raising a fatal <acronym>PHP</acronym> error.