[GENERIC] Zend_Translate:
[zend.git] / documentation / manual / en / module_specs / Zend_View-Helpers.xml
blob9f522b3ade68dc429bc6945df1935616003e7ffe
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!-- Reviewed: no -->
3 <sect1 id="zend.view.helpers" xmlns:xi="http://www.w3.org/2001/XInclude">
4     <title>View Helpers</title>
6     <para>
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.
11     </para>
13     <para>
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>.
22     </para>
24     <note>
25         <title>Watch the Case</title>
27         <para>
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.
31         </para>
32     </note>
34     <note>
35         <title>Default Helper Path</title>
37         <para>
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.
42         </para>
43     </note>
45     <para>
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>.
54     </para>
56     <sect2 id="zend.view.helpers.initial">
57         <title>Initial Helpers</title>
59         <para>
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:
65         </para>
67         <itemizedlist>
68             <listitem>
69                 <para>
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.
76                 </para>
77             </listitem>
79             <listitem>
80                 <para>
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
85                     the helper.
86                 </para>
87             </listitem>
89             <listitem>
90                 <para>
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
98                     generated.
99                 </para>
100             </listitem>
102             <listitem>
103                 <para>
104                     <methodname>formButton($name, $value, $attribs)</methodname>: Creates an
105                     &lt;button /&gt; element.
106                 </para>
107             </listitem>
109             <listitem>
110                 <para>
111                     <methodname>formCheckbox($name, $value, $attribs, $options)</methodname>:
112                     Creates an &lt;input type="checkbox" /&gt; element.
113                 </para>
115                 <para>
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
120                     passed.
121                 </para>
123                 <para>
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
128                     'unChecked'.
129                 </para>
131                 <para>
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'.
136                 </para>
138                 <para>
139                     The above is probably best summed up with some examples:
140                 </para>
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(
160     'checked' => 'bar',
161     'unChecked' => 'baz'
164 // 'bar' and 'baz' as checked/unchecked options; checked
165 echo $this->formCheckbox('foo', 'bar', null, array('bar', 'baz'));
166 echo $this->formCheckbox('foo',
167                          null,
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',
174                          null,
175                          array('checked' => false),
176                          array('bar', 'baz'));
177 ]]></programlisting>
179                 <para>
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.
183                 </para>
184             </listitem>
186             <listitem>
187                 <para>
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.
193                 </para>
195                 <para>
196                     You can specify alternate opening, closing, and separator
197                     content when rendering the errors by calling several methods
198                     on the helper:
199                 </para>
201                 <itemizedlist>
202                     <listitem>
203                         <para>
204                             <methodname>setElementStart($string)</methodname>; default is
205                             '&lt;ul class="errors"%s"&gt;&lt;li&gt;', where %s
206                             is replaced with the attributes as specified in
207                             <varname>$options</varname>.
208                         </para>
209                     </listitem>
211                     <listitem>
212                         <para>
213                             <methodname>setElementSeparator($string)</methodname>; default
214                             is '&lt;/li&gt;&lt;li&gt;'.
215                         </para>
216                     </listitem>
218                     <listitem>
219                         <para>
220                             <methodname>setElementEnd($string)</methodname>; default is
221                             '&lt;/li&gt;&lt;/ul&gt;'.
222                         </para>
223                     </listitem>
224                 </itemizedlist>
225             </listitem>
227             <listitem>
228                 <para>
229                     <methodname>formFile($name, $attribs)</methodname>: Creates an
230                     &lt;input type="file" /&gt; element.
231                 </para>
232             </listitem>
234             <listitem>
235                 <para>
236                     <methodname>formHidden($name, $value, $attribs)</methodname>: Creates an
237                     &lt;input type="hidden" /&gt; element.
238                 </para>
239             </listitem>
241             <listitem>
242                 <para>
243                     <methodname>formLabel($name, $value, $attribs)</methodname>: Creates a
244                     &lt;label&gt; 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.
248                 </para>
249             </listitem>
251             <listitem>
252                 <para>
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 ("&lt;br /&gt;") 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.
262                 </para>
263             </listitem>
265             <listitem>
266                 <para>
267                     <methodname>formPassword($name, $value, $attribs)</methodname>: Creates an
268                     &lt;input type="password" /&gt; element.
269                 </para>
270             </listitem>
272             <listitem>
273                 <para>
274                     <methodname>formRadio($name, $value, $attribs, $options)</methodname>:
275                     Creates a series of &lt;input type="radio" /&gt; 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.
279                 </para>
280             </listitem>
282             <listitem>
283                 <para>
284                     <methodname>formReset($name, $value, $attribs)</methodname>: Creates an
285                     &lt;input type="reset" /&gt; element.
286                 </para>
287             </listitem>
289             <listitem>
290                 <para>
291                     <methodname>formSelect($name, $value, $attribs, $options)</methodname>:
292                     Creates a &lt;select&gt;...&lt;/select&gt; block, with one
293                     &lt;option&gt;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
296                     preselected for you.
297                 </para>
298             </listitem>
300             <listitem>
301                 <para>
302                     <methodname>formSubmit($name, $value, $attribs)</methodname>: Creates an
303                     &lt;input type="submit" /&gt; element.
304                 </para>
305             </listitem>
307             <listitem>
308                 <para>
309                     <methodname>formText($name, $value, $attribs)</methodname>: Creates an
310                     &lt;input type="text" /&gt; element.
311                 </para>
312             </listitem>
314             <listitem>
315                 <para>
316                     <methodname>formTextarea($name, $value, $attribs)</methodname>: Creates a
317                     &lt;textarea&gt;...&lt;/textarea&gt; block.
318                 </para>
319             </listitem>
321             <listitem>
322                 <para>
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.
327                 </para>
328             </listitem>
330             <listitem>
331                 <para>
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.
339                 </para>
340             </listitem>
341         </itemizedlist>
343         <para>
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.
347         </para>
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)) ?>
359     </label></p>
360     <p><label>Your Country:
361 <?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
362     </label></p>
363     <p><label>Would you like to opt in?
364 <?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no')) ?>
365     </label></p>
366 </form>
367 ]]></programlisting>
369         <para>
370             The resulting output from the view script will look something like this:
371         </para>
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" />
377     </label></p>
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>
383         </select>
384     </label></p>
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" />
388     </label></p>
389 </form>
390 ]]></programlisting>
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" />
409     </sect2>
411     <sect2 id="zend.view.helpers.paths">
412         <title>Helper Paths</title>
414         <para>
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.
424         </para>
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');
431 ]]></programlisting>
433         <para>
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
439             helpers.
440         </para>
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".
456 ]]></programlisting>
457     </sect2>
459     <sect2 id="zend.view.helpers.custom">
460         <title>Writing Custom Helpers</title>
462         <para>
463             Writing custom helpers is easy; just follow these rules:
464         </para>
466         <itemizedlist>
467             <listitem>
468                 <para>
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.
477                 </para>
478             </listitem>
480             <listitem>
481                 <para>
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>).
491                 </para>
492             </listitem>
494             <listitem>
495                 <para>
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()".
501                 </para>
502             </listitem>
504             <listitem>
505                 <para>
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
509                     appropriately.
510                 </para>
511             </listitem>
513             <listitem>
514                 <para>
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".
518                 </para>
519             </listitem>
520         </itemizedlist>
522         <para>
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.
526         </para>
528         <para>
529             Here is an example of our <classname>SpecialPurpose</classname> helper code:
530         </para>
532         <programlisting language="php"><![CDATA[
533 class My_View_Helper_SpecialPurpose extends Zend_View_Helper_Abstract
535     protected $_count = 0;
536     public function specialPurpose()
537     {
538         $this->_count++;
539         $output = "I have seen 'The Jerk' {$this->_count} time(s).";
540         return htmlspecialchars($output);
541     }
543 ]]></programlisting>
545         <para>
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>
549             instance.
550         </para>
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();
557 ]]></programlisting>
559         <para>
560             The output would look something like this:
561         </para>
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).
567 ]]></programlisting>
569         <para>
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:
575         </para>
577         <programlisting language="php"><![CDATA[
578 class My_View_Helper_ScriptPath
580     public $view;
582     public function setView(Zend_View_Interface $view)
583     {
584         $this->view = $view;
585     }
587     public function scriptPath($script)
588     {
589         return $this->view->getScriptPath($script);
590     }
592 ]]></programlisting>
594         <para>
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.
599         </para>
601         <para>
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.
604         </para>
605     </sect2>
607     <sect2 id="zend.view.helpers.registering-concrete">
608         <title>Registering Concrete Helpers</title>
610         <para>
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.
615         </para>
617         <programlisting language="php"><![CDATA[
618 $helper = new My_Helper_Foo();
619 // ...do some configuration or dependency injection...
621 $view->registerHelper($helper, 'foo');
622 ]]></programlisting>
624         <para>
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.
627         </para>
629         <note>
630             <title>Helper name should match a method</title>
632             <para>
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.
637             </para>
638         </note>
639     </sect2>
640 </sect1>
641 <!--
642 vim:se ts=4 sw=4 et: