1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
|
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.view.helpers" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>View Helpers</title>
<para>
In your view scripts, often it is necessary to perform certain
complex functions over and over: e.g., formatting a date,
generating form elements, or displaying action links. You can
use helper classes to perform these behaviors for you.
</para>
<para>
A helper is simply a class. Let's say we want a helper named 'fooBar'.
By default, the class is prefixed with 'Zend_View_Helper_'
(you can specify a custom prefix when setting a helper path), and the
last segment of the class name is the helper name; this segment should
be TitleCapped; the full class name is then:
<classname>Zend_View_Helper_FooBar</classname>. This class should contain at the
minimum a single method, named after the helper, and camelCased:
<methodname>fooBar()</methodname>.
</para>
<note>
<title>Watch the Case</title>
<para>
Helper names are always camelCased, i.e., they never begin with an
uppercase character. The class name itself is MixedCased, but the
method that is actually executed is camelCased.
</para>
</note>
<note>
<title>Default Helper Path</title>
<para>
The default helper path always points to the Zend Framework view
helpers, i.e., 'Zend/View/Helper/'. Even if you call
<methodname>setHelperPath()</methodname> to overwrite the existing paths, this
path will be set to ensure the default helpers work.
</para>
</note>
<para>
To use a helper in your view script, call it using
<command>$this->helperName()</command>. Behind the scenes,
<classname>Zend_View</classname> will load the
<classname>Zend_View_Helper_HelperName</classname> class, create an object
instance of it, and call its <methodname>helperName()</methodname> method. The
object instance is persistent within the <classname>Zend_View</classname>
instance, and is reused for all future calls to
<command>$this->helperName()</command>.
</para>
<sect2 id="zend.view.helpers.initial">
<title>Initial Helpers</title>
<para>
<classname>Zend_View</classname> comes with an initial set of helper classes,
most of which relate to form element generation and perform
the appropriate output escaping automatically. In addition, there
are helpers for creating route-based <acronym>URL</acronym>s and <acronym>HTML</acronym>
lists, as well as declaring variables. The currently shipped helpers include:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>declareVars()</methodname>: Primarily for use when using
<methodname>strictVars()</methodname>, this helper can be used to declare
template variables that may or may not already be set in the
view object, as well as to set default values. Arrays passed as
arguments to the method will be used to set default values;
otherwise, if the variable does not exist, it is set to an empty string.
</para>
</listitem>
<listitem>
<para>
<methodname>fieldset($name, $content, $attribs)</methodname>: Creates an
<acronym>XHTML</acronym> fieldset. If <varname>$attribs</varname> contains a
'legend' key, that value will be used for the fieldset legend. The
fieldset will surround the <varname>$content</varname> as provided to
the helper.
</para>
</listitem>
<listitem>
<para>
<methodname>form($name, $attribs, $content)</methodname>: Generates an
<acronym>XHTML</acronym> form. All <varname>$attribs</varname> are escaped and
rendered as <acronym>XHTML</acronym> attributes of the form tag. If
<varname>$content</varname> is present and not a boolean
<constant>FALSE</constant>, then that content is rendered within the start and
close form tags; if <varname>$content</varname> is a boolean
<constant>FALSE</constant> (the default), only the opening form tag is
generated.
</para>
</listitem>
<listitem>
<para>
<methodname>formButton($name, $value, $attribs)</methodname>: Creates an
<button /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formCheckbox($name, $value, $attribs, $options)</methodname>:
Creates an <input type="checkbox" /> element.
</para>
<para>
By default, when no $value is provided and no $options are
present, '0' is assumed to be the unchecked value, and '1'
the checked value. If a $value is passed, but no $options
are present, the checked value is assumed to be the value
passed. The unchecked value is implemented by rendering a
hidden input element before rendering the checkbox.
</para>
<para>
$options should be an array. If the array is indexed, the
first value is the checked value, and the second the
unchecked value; all other values are ignored. You may also
pass an associative array with the keys 'checked' and
'unChecked'. The key 'disableHidden' can be set to true to
prevent rendering of the hidden field for the unchecked value.
</para>
<para>
If $options has been passed, if $value matches the checked
value, then the element will be marked as checked. You may
also mark the element as checked or unchecked by passing a
boolean value for the attribute 'checked'.
</para>
<para>
The above is probably best summed up with some examples:
</para>
<programlisting language="php"><![CDATA[
// '1' and '0' as checked/unchecked options; not checked
echo $this->formCheckbox('foo');
// '1' and '0' as checked/unchecked options; checked
echo $this->formCheckbox('foo', null, array('checked' => true));
// 'bar' and '0' as checked/unchecked options; not checked
echo $this->formCheckbox('foo', 'bar');
// 'bar' and '0' as checked/unchecked options; checked
echo $this->formCheckbox('foo', 'bar', array('checked' => true));
// 'bar' and 'baz' as checked/unchecked options; unchecked
echo $this->formCheckbox('foo', null, null, array('bar', 'baz'));
// 'bar' and 'baz' as checked/unchecked options; unchecked
echo $this->formCheckbox('foo', null, null, array(
'checked' => 'bar',
'unChecked' => 'baz'
));
// 'bar' and 'baz' as checked/unchecked options; checked
echo $this->formCheckbox('foo', 'bar', null, array('bar', 'baz'));
echo $this->formCheckbox('foo',
null,
array('checked' => true),
array('bar', 'baz'));
// 'bar' and 'baz' as checked/unchecked options; unchecked
echo $this->formCheckbox('foo', 'baz', null, array('bar', 'baz'));
echo $this->formCheckbox('foo',
null,
array('checked' => false),
array('bar', 'baz'));
]]></programlisting>
<para>
In all cases, the markup prepends a hidden element with the
unchecked value; this way, if the value is unchecked, you
will still get a valid value returned to your form.
</para>
</listitem>
<listitem>
<para>
<methodname>formErrors($errors, $options)</methodname>: Generates an
<acronym>XHTML</acronym> unordered list to show errors.
<varname>$errors</varname> should be a string or an array of strings;
<varname>$options</varname> should be any attributes you want
placed in the opening list tag.
</para>
<para>
You can specify alternate opening, closing, and separator
content when rendering the errors by calling several methods
on the helper:
</para>
<itemizedlist>
<listitem>
<para>
<methodname>setElementStart($string)</methodname>; default is
'<ul class="errors"%s"><li>', where %s
is replaced with the attributes as specified in
<varname>$options</varname>.
</para>
</listitem>
<listitem>
<para>
<methodname>setElementSeparator($string)</methodname>; default
is '</li><li>'.
</para>
</listitem>
<listitem>
<para>
<methodname>setElementEnd($string)</methodname>; default is
'</li></ul>'.
</para>
</listitem>
</itemizedlist>
</listitem>
<listitem>
<para>
<methodname>formFile($name, $attribs)</methodname>: Creates an
<input type="file" /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formHidden($name, $value, $attribs)</methodname>: Creates an
<input type="hidden" /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formImage($name, $value, $attribs)</methodname>: Creates an
<input type="image" /> element.
</para>
<programlisting language="php"><![CDATA[
echo $this->formImage(
'foo',
'bar',
array(
'src' => 'images/button.png',
'alt' => 'Button',
)
);
// Output: <input type="image" name="foo" id="foo" src="images/button.png" value="bar" alt="Button" />
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>formLabel($name, $value, $attribs)</methodname>: Creates a
<label> element, setting the <property>for</property> attribute to
<varname>$name</varname>, and the actual label text to
<varname>$value</varname>. If <emphasis>disable</emphasis> is passed in
<property>attribs</property>, nothing will be returned.
</para>
</listitem>
<listitem>
<para>
<methodname>formMultiCheckbox($name, $value, $attribs, $options,
$listsep)</methodname>: Creates a list of checkboxes.
<varname>$options</varname> should be an associative array, and may be
arbitrarily deep. <varname>$value</varname> may be a single value or
an array of selected values that match the keys in the
<varname>$options</varname> array. <varname>$listsep</varname> is an
<acronym>HTML</acronym> break ("<br />") by default. By default, this
element is treated as an array; all checkboxes share the same name, and are
submitted as an array.
</para>
<programlisting language="php"><![CDATA[
// Using list separator ($listsep):
echo '<ul><li>';
echo $view->formMultiCheckbox(
'foo',
2,
array(
'class' => 'baz',
),
array(
1 => 'One',
2 => 'Two',
3 => 'Three',
),
"</li>\n<li>"
);
echo '</li></ul>';
/*
Output:
<ul>
<li>
<label for="foo-1">
<input type="checkbox" name="foo[]" id="foo-1" value="1" class="baz" />One
</label>
</li>
<li>
<label for="foo-2">
<input type="checkbox" name="foo[]" id="foo-2" value="2" checked="checked" class="baz" />Two
</label>
</li>
<li>
<label for="foo-3">
<input type="checkbox" name="foo[]" id="foo-3" value="3" class="baz" />Three</label>
</li>
</ul>
*/
// Using options for label elements:
echo $this->formMultiCheckbox(
'foo',
2,
array(
'label_placement' => 'prepend',
'label_class' => 'baz',
),
array(
1 => 'One',
2 => 'Two',
3 => 'Three',
)
);
/*
Output:
<label class="baz" for="foo-1">
One<input type="checkbox" name="foo[]" id="foo-1" value="1" />
</label>
<br />
<label class="baz" for="foo-2">
Two<input type="checkbox" name="foo[]" id="foo-2" value="2" checked="checked" />
</label>
<br />
<label class="baz" for="foo-3">
Three<input type="checkbox" name="foo[]" id="foo-3" value="3" />
</label>
*/
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>formNote($name, $value = null)</methodname>: Creates a
simple text note. (e.g. as element for headlines in a
<classname>Zend_Form</classname> object)
</para>
<programlisting language="php"><![CDATA[
echo $this->formNote(null, 'This is an example text.');
// Output: This is an example text.
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>formPassword($name, $value, $attribs)</methodname>: Creates an
<input type="password" /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formRadio($name, $value, $attribs, $options, $listsep)</methodname>:
Creates a series of <input type="radio" /> elements, one
for each of the $options elements. In the $options array, the
element key is the radio value, and the element value is the
radio label. The $value radio will be preselected for you.
</para>
<programlisting language="php"><![CDATA[
// Using list separator ($listsep)
echo '<ul><li>';
echo $this->formRadio(
'foo',
2,
array(
'class' => 'baz',
),
array(
1 => 'One',
2 => 'Two',
3 => 'Three',
),
"</li>\n<li>"
);
echo '</li></ul>';
/*
Output:
<ul>
<li>
<label for="foo-1">
<input type="radio" name="foo" id="foo-1" value="1" class="baz" />One
</label>
</li>
<li>
<label for="foo-2">
<input type="radio" name="foo" id="foo-2" value="2" checked="checked" class="baz" />Two
</label>
</li>
<li>
<label for="foo-3">
<input type="radio" name="foo" id="foo-3" value="3" class="baz" />Three
</label>
</li>
</ul>
*/
// Using options for label elements:
echo $this->formRadio(
'foo',
2,
array(
'label_placement' => 'prepend',
'label_class' => 'baz',
),
array(
1 => 'One',
2 => 'Two',
3 => 'Three',
)
);
/*
Output:
<label class="baz" for="foo-1">
One<input type="radio" name="foo" id="foo-1" value="1" />
</label>
<br />
<label class="baz" for="foo-2">
Two<input type="radio" name="foo" id="foo-2" value="2" checked="checked" />
</label>
<br />
<label class="baz" for="foo-3">
Three<input type="radio" name="foo" id="foo-3" value="3" />
</label>
*/
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>formReset($name, $value, $attribs)</methodname>: Creates an
<input type="reset" /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formSelect($name, $value, $attribs, $options)</methodname>:
Creates a <select>...</select> block, with one
<option>one for each of the $options elements. In the
$options array, the element key is the option value, and the
element value is the option label. The $value option(s) will be
preselected for you.
</para>
<programlisting language="php"><![CDATA[
// Using option groups:
echo $view->formSelect(
'foo',
2,
array(
'class' => 'baz',
),
array(
1 => 'One',
'Two' => array(
'2.1' => 'One',
'2.2' => 'Two',
'2.3' => 'Three',
),
3 => 'Three',
)
);
/*
Output:
<select name="foo" id="foo" class="baz">
<option value="1" label="One">One</option>
<optgroup id="foo-optgroup-Two" label="Two">
<option value="2.1" label="One">One</option>
<option value="2.2" label="Two">Two</option>
<option value="2.3" label="Three">Three</option>
</optgroup>
<option value="3" label="Three">Three</option>
</select>
*/
// First example with 'multiple' option:
echo $this->formSelect(
'foo[]',
2,
null,
array(
1 => 'One',
2 => 'Two',
3 => 'Three',
)
);
/*
Output:
<select name="foo[]" id="foo" multiple="multiple">
<option value="1" label="One">One</option>
<option value="2" label="Two" selected="selected">Two</option>
<option value="3" label="Three">Three</option>
</select>
*/
// Second example with 'multiple' option:
echo $this->formSelect(
'foo',
array(
1,
2,
),
array(
'multiple' => true,
),
array(
1 => 'One',
2 => 'Two',
3 => 'Three',
)
);
/*
Output:
<select name="foo[]" id="foo" multiple="multiple">
<option value="1" label="One" selected="selected">One</option>
<option value="2" label="Two" selected="selected">Two</option>
<option value="3" label="Three">Three</option>
</select>
*/
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>formSubmit($name, $value, $attribs)</methodname>: Creates an
<input type="submit" /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formText($name, $value, $attribs)</methodname>: Creates an
<input type="text" /> element.
</para>
</listitem>
<listitem>
<para>
<methodname>formTextarea($name, $value, $attribs)</methodname>: Creates a
<textarea>...</textarea> block.
</para>
</listitem>
<listitem>
<para>
<methodname>url($urlOptions, $name, $reset, $encode)</methodname>: Creates a
<acronym>URL</acronym> string based on a named route.
<varname>$urlOptions</varname> should be an associative array of key/value pairs
used by the particular route.
</para>
<programlisting language="php"><![CDATA[
// Using without options: (current request is: user/id/1)
echo $this->url();
// Output: user/info/id/1
// Set URL options:
echo $this->url(
array('controller' => 'user', 'action' => 'info', 'username' => 'foobar')
);
// Output: user/info/username/foobar
// Using a route:
$router->addRoute(
'user',
new Zend_Controller_Router_Route(
'user/:username',
array(
'controller' => 'user',
'action' => 'info',
)
)
);
echo $this->url(array('name' => 'foobar'), 'user');
// Output: user/foobar
// Using reset: (current request is: user/id/1)
echo $this->url(array('controller' => 'user', 'action' => 'info'), null, false);
// Output: user/info/id/1
echo $this->url(array('controller' => 'user', 'action' => 'info'), null, true);
// Output: user/info
// Using encode:
echo $this->url(
array('controller' => 'user', 'action' => 'info', 'username' => 'John Doe'), null, true, false
);
// Output: user/info/username/John Doe
echo $this->url(
array('controller' => 'user', 'action' => 'info', 'username' => 'John Doe'), null, true, false
);
// Output: user/info/username/John+Doe
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>serverUrl($requestUri = null)</methodname>: Helper
for returning the current server URL (optionally with request URI).
</para>
<programlisting language="php"><![CDATA[
// Current server URL in the example is: http://www.example.com/foo.html
echo $this->serverUrl();
// Output: http://www.example.com
echo $this->serverUrl(true);
// Output: http://www.example.com/foo.html
echo $this->serverUrl('/foo/bar');
// Output: http://www.example.com/foo/bar
echo $this->serverUrl()->getHost();
// Output: www.example.com
echo $this->serverUrl()->getScheme();
// Output: http
$this->serverUrl()->setHost('www.foo.com');
$this->serverUrl()->setScheme('https');
echo $this->serverUrl();
// Output: https://www.foo.com
]]></programlisting>
</listitem>
<listitem>
<para>
<methodname>htmlList($items, $ordered, $attribs, $escape)</methodname>:
generates unordered and ordered lists based on the <varname>$items</varname>
passed to it. If <varname>$items</varname> is a multidimensional
array, a nested list will be built. If the <varname>$escape</varname>
flag is <constant>TRUE</constant> (default), individual items will be escaped
using the view objects registered escaping mechanisms; pass a
<constant>FALSE</constant> value if you want to allow markup in your lists.
</para>
</listitem>
</itemizedlist>
<para>
Using these in your view scripts is very easy, here is an example.
Note that you all you need to do is call them; they will load
and instantiate themselves as they are needed.
</para>
<programlisting language="php"><![CDATA[
// inside your view script, $this refers to the Zend_View instance.
//
// say that you have already assigned a series of select options under
// the name $countries as array('us' => 'United States', 'il' =>
// 'Israel', 'de' => 'Germany').
?>
<form action="action.php" method="post">
<p><label>Your Email:
<?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
</label></p>
<p><label>Your Country:
<?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
</label></p>
<p><label>Would you like to opt in?
<?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no')) ?>
</label></p>
</form>
]]></programlisting>
<para>
The resulting output from the view script will look something like this:
</para>
<programlisting language="php"><![CDATA[
<form action="action.php" method="post">
<p><label>Your Email:
<input type="text" name="email" value="you@example.com" size="32" />
</label></p>
<p><label>Your Country:
<select name="country">
<option value="us" selected="selected">United States</option>
<option value="il">Israel</option>
<option value="de">Germany</option>
</select>
</label></p>
<p><label>Would you like to opt in?
<input type="hidden" name="opt_in" value="no" />
<input type="checkbox" name="opt_in" value="yes" checked="checked" />
</label></p>
</form>
]]></programlisting>
<xi:include href="Zend_View-Helpers-Action.xml" />
<xi:include href="Zend_View-Helpers-BaseUrl.xml" />
<xi:include href="Zend_View-Helpers-Currency.xml" />
<xi:include href="Zend_View-Helpers-Cycle.xml" />
<xi:include href="Zend_View-Helpers-Partial.xml" />
<xi:include href="Zend_View-Helpers-Placeholder.xml" />
<xi:include href="Zend_View-Helpers-Doctype.xml" />
<xi:include href="Zend_View-Helpers-Gravatar.xml" />
<xi:include href="Zend_View-Helpers-HeadLink.xml" />
<xi:include href="Zend_View-Helpers-HeadMeta.xml" />
<xi:include href="Zend_View-Helpers-HeadScript.xml" />
<xi:include href="Zend_View-Helpers-HeadStyle.xml" />
<xi:include href="Zend_View-Helpers-HeadTitle.xml" />
<xi:include href="Zend_View-Helpers-HtmlObject.xml" />
<xi:include href="Zend_View-Helpers-InlineScript.xml" />
<xi:include href="Zend_View-Helpers-RenderToPlaceholder.xml" />
<xi:include href="Zend_View-Helpers-Json.xml" />
<xi:include href="Zend_View-Helpers-Navigation.xml" />
<xi:include href="Zend_View-Helpers-Translate.xml" />
<xi:include href="Zend_View-Helpers-UserAgent.xml" />
</sect2>
<sect2 id="zend.view.helpers.paths">
<title>Helper Paths</title>
<para>
As with view scripts, your controller can specify a stack of paths
for <classname>Zend_View</classname> to search for helper classes. By default,
<classname>Zend_View</classname> looks in "Zend/View/Helper/*" for helper
classes. You can tell <classname>Zend_View</classname> to look in other
locations using the <methodname>setHelperPath()</methodname> and
<methodname>addHelperPath()</methodname> methods. Additionally, you can
indicate a class prefix to use for helpers in the path provided, to
allow namespacing your helper classes. By default, if no class
prefix is provided, 'Zend_View_Helper_' is assumed.
</para>
<programlisting language="php"><![CDATA[
$view = new Zend_View();
// Set path to /path/to/more/helpers, with prefix 'My_View_Helper'
$view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');
]]></programlisting>
<para>
In fact, you can "stack" paths using the
<methodname>addHelperPath()</methodname> method. As you add paths to the stack,
<classname>Zend_View</classname> will look at the most-recently-added path for
the requested helper class. This allows you to add to (or even
override) the initial distribution of helpers with your own custom
helpers.
</para>
<programlisting language="php"><![CDATA[
$view = new Zend_View();
// Add /path/to/some/helpers with class prefix 'My_View_Helper'
$view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
// Add /other/path/to/helpers with class prefix 'Your_View_Helper'
$view->addHelperPath('/other/path/to/helpers', 'Your_View_Helper');
// now when you call $this->helperName(), Zend_View will look first for
// "/path/to/some/helpers/HelperName" using class name
// "Your_View_Helper_HelperName", then for
// "/other/path/to/helpers/HelperName.php" using class name
// "My_View_Helper_HelperName", and finally for
// "Zend/View/Helper/HelperName.php" using class name
// "Zend_View_Helper_HelperName".
]]></programlisting>
</sect2>
<sect2 id="zend.view.helpers.custom">
<title>Writing Custom Helpers</title>
<para>
Writing custom helpers is easy; just follow these rules:
</para>
<itemizedlist>
<listitem>
<para>
While not strictly necessary, we recommend either implementing
<classname>Zend_View_Helper_Interface</classname> or extending
<classname>Zend_View_Helper_Abstract</classname> when creating your
helpers. Introduced in 1.6.0, these simply define a
<methodname>setView()</methodname> method; however, in upcoming releases, we
plan to implement a strategy pattern that will simplify much of
the naming schema detailed below. Building off these now will
help you future-proof your code.
</para>
</listitem>
<listitem>
<para>
The class name must, at the very minimum, end with the helper
name itself, using MixedCaps. E.g., if you were writing a
helper called "specialPurpose", the class name would minimally
need to be "SpecialPurpose". You may, and should, give the class
name a prefix, and it is recommended that you use 'View_Helper'
as part of that prefix: "My_View_Helper_SpecialPurpose". (You
will need to pass in the prefix, with or without the trailing
underscore, to <methodname>addHelperPath()</methodname> or
<methodname>setHelperPath()</methodname>).
</para>
</listitem>
<listitem>
<para>
The class must have a public method that matches the
helper name; this is the method that will be called when
your template calls "$this->specialPurpose()". In our
"specialPurpose" helper example, the required method
declaration would be "public function specialPurpose()".
</para>
</listitem>
<listitem>
<para>
In general, the class should not echo or print or otherwise
generate output. Instead, it should return values to be
printed or echoed. The returned values should be escaped
appropriately.
</para>
</listitem>
<listitem>
<para>
The class must be in a file named after the helper class. Again
using our "specialPurpose" helper example, the file has to be
named "SpecialPurpose.php".
</para>
</listitem>
</itemizedlist>
<para>
Place the helper class file somewhere in your helper path stack, and
<classname>Zend_View</classname> will automatically load, instantiate,
persist, and execute it for you.
</para>
<para>
Here is an example of our <classname>SpecialPurpose</classname> helper code:
</para>
<programlisting language="php"><![CDATA[
class My_View_Helper_SpecialPurpose extends Zend_View_Helper_Abstract
{
protected $_count = 0;
public function specialPurpose()
{
$this->_count++;
$output = "I have seen 'The Jerk' {$this->_count} time(s).";
return htmlspecialchars($output);
}
}
]]></programlisting>
<para>
Then in a view script, you can call the <classname>SpecialPurpose</classname>
helper as many times as you like; it will be instantiated once, and
then it persists for the life of that <classname>Zend_View</classname>
instance.
</para>
<programlisting language="php"><![CDATA[
// remember, in a view script, $this refers to the Zend_View instance.
echo $this->specialPurpose();
echo $this->specialPurpose();
echo $this->specialPurpose();
]]></programlisting>
<para>
The output would look something like this:
</para>
<programlisting language="php"><![CDATA[
I have seen 'The Jerk' 1 time(s).
I have seen 'The Jerk' 2 time(s).
I have seen 'The Jerk' 3 time(s).
]]></programlisting>
<para>
Sometimes you will need access to the calling <classname>Zend_View</classname>
object -- for instance, if you need to use the registered encoding,
or want to render another view script as part of your helper. To get
access to the view object, your helper class should have a
<methodname>setView($view)</methodname> method, like the following:
</para>
<programlisting language="php"><![CDATA[
class My_View_Helper_ScriptPath
{
public $view;
public function setView(Zend_View_Interface $view)
{
$this->view = $view;
}
public function scriptPath($script)
{
return $this->view->getScriptPath($script);
}
}
]]></programlisting>
<para>
If your helper class has a <methodname>setView()</methodname> method, it will be
called when the helper class is first instantiated, and passed the
current view object. It is up to you to persist the object in your
class, as well as determine how it should be accessed.
</para>
<para>
If you are extending <classname>Zend_View_Helper_Abstract</classname>, you do
not need to define this method, as it is defined for you.
</para>
</sect2>
<sect2 id="zend.view.helpers.registering-concrete">
<title>Registering Concrete Helpers</title>
<para>
Sometimes it is convenient to instantiate a view helper, and then register it with the
view. As of version 1.10.0, this is now possible using the
<methodname>registerHelper()</methodname> method, which expects two arguments: the
helper object, and the name by which it will be registered.
</para>
<programlisting language="php"><![CDATA[
$helper = new My_Helper_Foo();
// ...do some configuration or dependency injection...
$view->registerHelper($helper, 'foo');
]]></programlisting>
<para>
If the helper has a <methodname>setView()</methodname> method, the view object will call
this and inject itself into the helper on registration.
</para>
<note>
<title>Helper name should match a method</title>
<para>
The second argument to <methodname>registerHelper()</methodname> is the name of the
helper. A corresponding method name should exist in the helper; otherwise,
<classname>Zend_View</classname> will call a non-existent method when invoking the
helper, raising a fatal <acronym>PHP</acronym> error.
</para>
</note>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
|