Zend_View-Helpers.xml 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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>
  5. <para>
  6. In your view scripts, often it is necessary to perform certain
  7. complex functions over and over: e.g., formatting a date,
  8. generating form elements, or displaying action links. You can
  9. use helper classes to perform these behaviors for you.
  10. </para>
  11. <para>
  12. A helper is simply a class. Let's say we want a helper named 'fooBar'.
  13. By default, the class is prefixed with 'Zend_View_Helper_'
  14. (you can specify a custom prefix when setting a helper path), and the
  15. last segment of the class name is the helper name; this segment should
  16. be TitleCapped; the full class name is then:
  17. <classname>Zend_View_Helper_FooBar</classname>. This class should contain at the
  18. minimum a single method, named after the helper, and camelCased:
  19. <methodname>fooBar()</methodname>.
  20. </para>
  21. <note>
  22. <title>Watch the Case</title>
  23. <para>
  24. Helper names are always camelCased, i.e., they never begin with an
  25. uppercase character. The class name itself is MixedCased, but the
  26. method that is actually executed is camelCased.
  27. </para>
  28. </note>
  29. <note>
  30. <title>Default Helper Path</title>
  31. <para>
  32. The default helper path always points to the Zend Framework view
  33. helpers, i.e., 'Zend/View/Helper/'. Even if you call
  34. <methodname>setHelperPath()</methodname> to overwrite the existing paths, this
  35. path will be set to ensure the default helpers work.
  36. </para>
  37. </note>
  38. <para>
  39. To use a helper in your view script, call it using
  40. <command>$this->helperName()</command>. Behind the scenes,
  41. <classname>Zend_View</classname> will load the
  42. <classname>Zend_View_Helper_HelperName</classname> class, create an object
  43. instance of it, and call its <methodname>helperName()</methodname> method. The
  44. object instance is persistent within the <classname>Zend_View</classname>
  45. instance, and is reused for all future calls to
  46. <command>$this->helperName()</command>.
  47. </para>
  48. <sect2 id="zend.view.helpers.initial">
  49. <title>Initial Helpers</title>
  50. <para>
  51. <classname>Zend_View</classname> comes with an initial set of helper classes,
  52. most of which relate to form element generation and perform
  53. the appropriate output escaping automatically. In addition, there
  54. are helpers for creating route-based <acronym>URL</acronym>s and <acronym>HTML</acronym>
  55. lists, as well as declaring variables. The currently shipped helpers include:
  56. </para>
  57. <itemizedlist>
  58. <listitem>
  59. <para>
  60. <methodname>declareVars()</methodname>: Primarily for use when using
  61. <methodname>strictVars()</methodname>, this helper can be used to declare
  62. template variables that may or may not already be set in the
  63. view object, as well as to set default values. Arrays passed as
  64. arguments to the method will be used to set default values;
  65. otherwise, if the variable does not exist, it is set to an empty string.
  66. </para>
  67. </listitem>
  68. <listitem>
  69. <para>
  70. <methodname>fieldset($name, $content, $attribs)</methodname>: Creates an
  71. <acronym>XHTML</acronym> fieldset. If <varname>$attribs</varname> contains a
  72. 'legend' key, that value will be used for the fieldset legend. The
  73. fieldset will surround the <varname>$content</varname> as provided to
  74. the helper.
  75. </para>
  76. </listitem>
  77. <listitem>
  78. <para>
  79. <methodname>form($name, $attribs, $content)</methodname>: Generates an
  80. <acronym>XHTML</acronym> form. All <varname>$attribs</varname> are escaped and
  81. rendered as <acronym>XHTML</acronym> attributes of the form tag. If
  82. <varname>$content</varname> is present and not a boolean
  83. <constant>FALSE</constant>, then that content is rendered within the start and
  84. close form tags; if <varname>$content</varname> is a boolean
  85. <constant>FALSE</constant> (the default), only the opening form tag is
  86. generated.
  87. </para>
  88. </listitem>
  89. <listitem>
  90. <para>
  91. <methodname>formButton($name, $value, $attribs)</methodname>: Creates an
  92. &lt;button /&gt; element.
  93. </para>
  94. </listitem>
  95. <listitem>
  96. <para>
  97. <methodname>formCheckbox($name, $value, $attribs, $options)</methodname>:
  98. Creates an &lt;input type="checkbox" /&gt; element.
  99. </para>
  100. <para>
  101. By default, when no $value is provided and no $options are
  102. present, '0' is assumed to be the unchecked value, and '1'
  103. the checked value. If a $value is passed, but no $options
  104. are present, the checked value is assumed to be the value
  105. passed. The unchecked value is implemented by rendering a
  106. hidden input element before rendering the checkbox.
  107. </para>
  108. <para>
  109. $options should be an array. If the array is indexed, the
  110. first value is the checked value, and the second the
  111. unchecked value; all other values are ignored. You may also
  112. pass an associative array with the keys 'checked' and
  113. 'unChecked'. The key 'disableHidden' can be set to true to
  114. prevent rendering of the hidden field for the unchecked value.
  115. </para>
  116. <para>
  117. If $options has been passed, if $value matches the checked
  118. value, then the element will be marked as checked. You may
  119. also mark the element as checked or unchecked by passing a
  120. boolean value for the attribute 'checked'.
  121. </para>
  122. <para>
  123. The above is probably best summed up with some examples:
  124. </para>
  125. <programlisting language="php"><![CDATA[
  126. // '1' and '0' as checked/unchecked options; not checked
  127. echo $this->formCheckbox('foo');
  128. // '1' and '0' as checked/unchecked options; checked
  129. echo $this->formCheckbox('foo', null, array('checked' => true));
  130. // 'bar' and '0' as checked/unchecked options; not checked
  131. echo $this->formCheckbox('foo', 'bar');
  132. // 'bar' and '0' as checked/unchecked options; checked
  133. echo $this->formCheckbox('foo', 'bar', array('checked' => true));
  134. // 'bar' and 'baz' as checked/unchecked options; unchecked
  135. echo $this->formCheckbox('foo', null, null, array('bar', 'baz'));
  136. // 'bar' and 'baz' as checked/unchecked options; unchecked
  137. echo $this->formCheckbox('foo', null, null, array(
  138. 'checked' => 'bar',
  139. 'unChecked' => 'baz'
  140. ));
  141. // 'bar' and 'baz' as checked/unchecked options; checked
  142. echo $this->formCheckbox('foo', 'bar', null, array('bar', 'baz'));
  143. echo $this->formCheckbox('foo',
  144. null,
  145. array('checked' => true),
  146. array('bar', 'baz'));
  147. // 'bar' and 'baz' as checked/unchecked options; unchecked
  148. echo $this->formCheckbox('foo', 'baz', null, array('bar', 'baz'));
  149. echo $this->formCheckbox('foo',
  150. null,
  151. array('checked' => false),
  152. array('bar', 'baz'));
  153. ]]></programlisting>
  154. <para>
  155. In all cases, the markup prepends a hidden element with the
  156. unchecked value; this way, if the value is unchecked, you
  157. will still get a valid value returned to your form.
  158. </para>
  159. </listitem>
  160. <listitem>
  161. <para>
  162. <methodname>formErrors($errors, $options)</methodname>: Generates an
  163. <acronym>XHTML</acronym> unordered list to show errors.
  164. <varname>$errors</varname> should be a string or an array of strings;
  165. <varname>$options</varname> should be any attributes you want
  166. placed in the opening list tag.
  167. </para>
  168. <para>
  169. You can specify alternate opening, closing, and separator
  170. content when rendering the errors by calling several methods
  171. on the helper:
  172. </para>
  173. <itemizedlist>
  174. <listitem>
  175. <para>
  176. <methodname>setElementStart($string)</methodname>; default is
  177. '&lt;ul class="errors"%s"&gt;&lt;li&gt;', where %s
  178. is replaced with the attributes as specified in
  179. <varname>$options</varname>.
  180. </para>
  181. </listitem>
  182. <listitem>
  183. <para>
  184. <methodname>setElementSeparator($string)</methodname>; default
  185. is '&lt;/li&gt;&lt;li&gt;'.
  186. </para>
  187. </listitem>
  188. <listitem>
  189. <para>
  190. <methodname>setElementEnd($string)</methodname>; default is
  191. '&lt;/li&gt;&lt;/ul&gt;'.
  192. </para>
  193. </listitem>
  194. </itemizedlist>
  195. </listitem>
  196. <listitem>
  197. <para>
  198. <methodname>formFile($name, $attribs)</methodname>: Creates an
  199. &lt;input type="file" /&gt; element.
  200. </para>
  201. </listitem>
  202. <listitem>
  203. <para>
  204. <methodname>formHidden($name, $value, $attribs)</methodname>: Creates an
  205. &lt;input type="hidden" /&gt; element.
  206. </para>
  207. </listitem>
  208. <listitem>
  209. <para>
  210. <methodname>formLabel($name, $value, $attribs)</methodname>: Creates a
  211. &lt;label&gt; element, setting the <property>for</property> attribute to
  212. <varname>$name</varname>, and the actual label text to
  213. <varname>$value</varname>. If <emphasis>disable</emphasis> is passed in
  214. <property>attribs</property>, nothing will be returned.
  215. </para>
  216. </listitem>
  217. <listitem>
  218. <para>
  219. <methodname>formMultiCheckbox($name, $value, $attribs, $options,
  220. $listsep)</methodname>: Creates a list of checkboxes.
  221. <varname>$options</varname> should be an associative array, and may be
  222. arbitrarily deep. <varname>$value</varname> may be a single value or
  223. an array of selected values that match the keys in the
  224. <varname>$options</varname> array. <varname>$listsep</varname> is an
  225. <acronym>HTML</acronym> break ("&lt;br /&gt;") by default. By default, this
  226. element is treated as an array; all checkboxes share the same name, and are
  227. submitted as an array.
  228. </para>
  229. </listitem>
  230. <listitem>
  231. <para>
  232. <methodname>formPassword($name, $value, $attribs)</methodname>: Creates an
  233. &lt;input type="password" /&gt; element.
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <methodname>formRadio($name, $value, $attribs, $options)</methodname>:
  239. Creates a series of &lt;input type="radio" /&gt; elements, one
  240. for each of the $options elements. In the $options array, the
  241. element key is the radio value, and the element value is the
  242. radio label. The $value radio will be preselected for you.
  243. </para>
  244. </listitem>
  245. <listitem>
  246. <para>
  247. <methodname>formReset($name, $value, $attribs)</methodname>: Creates an
  248. &lt;input type="reset" /&gt; element.
  249. </para>
  250. </listitem>
  251. <listitem>
  252. <para>
  253. <methodname>formSelect($name, $value, $attribs, $options)</methodname>:
  254. Creates a &lt;select&gt;...&lt;/select&gt; block, with one
  255. &lt;option&gt;one for each of the $options elements. In the
  256. $options array, the element key is the option value, and the
  257. element value is the option label. The $value option(s) will be
  258. preselected for you.
  259. </para>
  260. </listitem>
  261. <listitem>
  262. <para>
  263. <methodname>formSubmit($name, $value, $attribs)</methodname>: Creates an
  264. &lt;input type="submit" /&gt; element.
  265. </para>
  266. </listitem>
  267. <listitem>
  268. <para>
  269. <methodname>formText($name, $value, $attribs)</methodname>: Creates an
  270. &lt;input type="text" /&gt; element.
  271. </para>
  272. </listitem>
  273. <listitem>
  274. <para>
  275. <methodname>formTextarea($name, $value, $attribs)</methodname>: Creates a
  276. &lt;textarea&gt;...&lt;/textarea&gt; block.
  277. </para>
  278. </listitem>
  279. <listitem>
  280. <para>
  281. <methodname>url($urlOptions, $name, $reset)</methodname>: Creates a
  282. <acronym>URL</acronym> string based on a named route.
  283. <varname>$urlOptions</varname> should be an associative array of key/value pairs
  284. used by the particular route.
  285. </para>
  286. </listitem>
  287. <listitem>
  288. <para>
  289. <methodname>htmlList($items, $ordered, $attribs, $escape)</methodname>:
  290. generates unordered and ordered lists based on the <varname>$items</varname>
  291. passed to it. If <varname>$items</varname> is a multidimensional
  292. array, a nested list will be built. If the <varname>$escape</varname>
  293. flag is <constant>TRUE</constant> (default), individual items will be escaped
  294. using the view objects registered escaping mechanisms; pass a
  295. <constant>FALSE</constant> value if you want to allow markup in your lists.
  296. </para>
  297. </listitem>
  298. </itemizedlist>
  299. <para>
  300. Using these in your view scripts is very easy, here is an example.
  301. Note that you all you need to do is call them; they will load
  302. and instantiate themselves as they are needed.
  303. </para>
  304. <programlisting language="php"><![CDATA[
  305. // inside your view script, $this refers to the Zend_View instance.
  306. //
  307. // say that you have already assigned a series of select options under
  308. // the name $countries as array('us' => 'United States', 'il' =>
  309. // 'Israel', 'de' => 'Germany').
  310. ?>
  311. <form action="action.php" method="post">
  312. <p><label>Your Email:
  313. <?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
  314. </label></p>
  315. <p><label>Your Country:
  316. <?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
  317. </label></p>
  318. <p><label>Would you like to opt in?
  319. <?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no')) ?>
  320. </label></p>
  321. </form>
  322. ]]></programlisting>
  323. <para>
  324. The resulting output from the view script will look something like this:
  325. </para>
  326. <programlisting language="php"><![CDATA[
  327. <form action="action.php" method="post">
  328. <p><label>Your Email:
  329. <input type="text" name="email" value="you@example.com" size="32" />
  330. </label></p>
  331. <p><label>Your Country:
  332. <select name="country">
  333. <option value="us" selected="selected">United States</option>
  334. <option value="il">Israel</option>
  335. <option value="de">Germany</option>
  336. </select>
  337. </label></p>
  338. <p><label>Would you like to opt in?
  339. <input type="hidden" name="opt_in" value="no" />
  340. <input type="checkbox" name="opt_in" value="yes" checked="checked" />
  341. </label></p>
  342. </form>
  343. ]]></programlisting>
  344. <xi:include href="Zend_View-Helpers-Action.xml" />
  345. <xi:include href="Zend_View-Helpers-BaseUrl.xml" />
  346. <xi:include href="Zend_View-Helpers-Currency.xml" />
  347. <xi:include href="Zend_View-Helpers-Cycle.xml" />
  348. <xi:include href="Zend_View-Helpers-Partial.xml" />
  349. <xi:include href="Zend_View-Helpers-Placeholder.xml" />
  350. <xi:include href="Zend_View-Helpers-Doctype.xml" />
  351. <xi:include href="Zend_View-Helpers-Gravatar.xml" />
  352. <xi:include href="Zend_View-Helpers-HeadLink.xml" />
  353. <xi:include href="Zend_View-Helpers-HeadMeta.xml" />
  354. <xi:include href="Zend_View-Helpers-HeadScript.xml" />
  355. <xi:include href="Zend_View-Helpers-HeadStyle.xml" />
  356. <xi:include href="Zend_View-Helpers-HeadTitle.xml" />
  357. <xi:include href="Zend_View-Helpers-HtmlObject.xml" />
  358. <xi:include href="Zend_View-Helpers-InlineScript.xml" />
  359. <xi:include href="Zend_View-Helpers-Json.xml" />
  360. <xi:include href="Zend_View-Helpers-Navigation.xml" />
  361. <xi:include href="Zend_View-Helpers-TinySrc.xml" />
  362. <xi:include href="Zend_View-Helpers-Translate.xml" />
  363. <xi:include href="Zend_View-Helpers-UserAgent.xml" />
  364. </sect2>
  365. <sect2 id="zend.view.helpers.paths">
  366. <title>Helper Paths</title>
  367. <para>
  368. As with view scripts, your controller can specify a stack of paths
  369. for <classname>Zend_View</classname> to search for helper classes. By default,
  370. <classname>Zend_View</classname> looks in "Zend/View/Helper/*" for helper
  371. classes. You can tell <classname>Zend_View</classname> to look in other
  372. locations using the <methodname>setHelperPath()</methodname> and
  373. <methodname>addHelperPath()</methodname> methods. Additionally, you can
  374. indicate a class prefix to use for helpers in the path provided, to
  375. allow namespacing your helper classes. By default, if no class
  376. prefix is provided, 'Zend_View_Helper_' is assumed.
  377. </para>
  378. <programlisting language="php"><![CDATA[
  379. $view = new Zend_View();
  380. // Set path to /path/to/more/helpers, with prefix 'My_View_Helper'
  381. $view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');
  382. ]]></programlisting>
  383. <para>
  384. In fact, you can "stack" paths using the
  385. <methodname>addHelperPath()</methodname> method. As you add paths to the stack,
  386. <classname>Zend_View</classname> will look at the most-recently-added path for
  387. the requested helper class. This allows you to add to (or even
  388. override) the initial distribution of helpers with your own custom
  389. helpers.
  390. </para>
  391. <programlisting language="php"><![CDATA[
  392. $view = new Zend_View();
  393. // Add /path/to/some/helpers with class prefix 'My_View_Helper'
  394. $view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
  395. // Add /other/path/to/helpers with class prefix 'Your_View_Helper'
  396. $view->addHelperPath('/other/path/to/helpers', 'Your_View_Helper');
  397. // now when you call $this->helperName(), Zend_View will look first for
  398. // "/path/to/some/helpers/HelperName" using class name
  399. // "Your_View_Helper_HelperName", then for
  400. // "/other/path/to/helpers/HelperName.php" using class name
  401. // "My_View_Helper_HelperName", and finally for
  402. // "Zend/View/Helper/HelperName.php" using class name
  403. // "Zend_View_Helper_HelperName".
  404. ]]></programlisting>
  405. </sect2>
  406. <sect2 id="zend.view.helpers.custom">
  407. <title>Writing Custom Helpers</title>
  408. <para>
  409. Writing custom helpers is easy; just follow these rules:
  410. </para>
  411. <itemizedlist>
  412. <listitem>
  413. <para>
  414. While not strictly necessary, we recommend either implementing
  415. <classname>Zend_View_Helper_Interface</classname> or extending
  416. <classname>Zend_View_Helper_Abstract</classname> when creating your
  417. helpers. Introduced in 1.6.0, these simply define a
  418. <methodname>setView()</methodname> method; however, in upcoming releases, we
  419. plan to implement a strategy pattern that will simplify much of
  420. the naming schema detailed below. Building off these now will
  421. help you future-proof your code.
  422. </para>
  423. </listitem>
  424. <listitem>
  425. <para>
  426. The class name must, at the very minimum, end with the helper
  427. name itself, using MixedCaps. E.g., if you were writing a
  428. helper called "specialPurpose", the class name would minimally
  429. need to be "SpecialPurpose". You may, and should, give the class
  430. name a prefix, and it is recommended that you use 'View_Helper'
  431. as part of that prefix: "My_View_Helper_SpecialPurpose". (You
  432. will need to pass in the prefix, with or without the trailing
  433. underscore, to <methodname>addHelperPath()</methodname> or
  434. <methodname>setHelperPath()</methodname>).
  435. </para>
  436. </listitem>
  437. <listitem>
  438. <para>
  439. The class must have a public method that matches the
  440. helper name; this is the method that will be called when
  441. your template calls "$this->specialPurpose()". In our
  442. "specialPurpose" helper example, the required method
  443. declaration would be "public function specialPurpose()".
  444. </para>
  445. </listitem>
  446. <listitem>
  447. <para>
  448. In general, the class should not echo or print or otherwise
  449. generate output. Instead, it should return values to be
  450. printed or echoed. The returned values should be escaped
  451. appropriately.
  452. </para>
  453. </listitem>
  454. <listitem>
  455. <para>
  456. The class must be in a file named after the helper class. Again
  457. using our "specialPurpose" helper example, the file has to be
  458. named "SpecialPurpose.php".
  459. </para>
  460. </listitem>
  461. </itemizedlist>
  462. <para>
  463. Place the helper class file somewhere in your helper path stack, and
  464. <classname>Zend_View</classname> will automatically load, instantiate,
  465. persist, and execute it for you.
  466. </para>
  467. <para>
  468. Here is an example of our <classname>SpecialPurpose</classname> helper code:
  469. </para>
  470. <programlisting language="php"><![CDATA[
  471. class My_View_Helper_SpecialPurpose extends Zend_View_Helper_Abstract
  472. {
  473. protected $_count = 0;
  474. public function specialPurpose()
  475. {
  476. $this->_count++;
  477. $output = "I have seen 'The Jerk' {$this->_count} time(s).";
  478. return htmlspecialchars($output);
  479. }
  480. }
  481. ]]></programlisting>
  482. <para>
  483. Then in a view script, you can call the <classname>SpecialPurpose</classname>
  484. helper as many times as you like; it will be instantiated once, and
  485. then it persists for the life of that <classname>Zend_View</classname>
  486. instance.
  487. </para>
  488. <programlisting language="php"><![CDATA[
  489. // remember, in a view script, $this refers to the Zend_View instance.
  490. echo $this->specialPurpose();
  491. echo $this->specialPurpose();
  492. echo $this->specialPurpose();
  493. ]]></programlisting>
  494. <para>
  495. The output would look something like this:
  496. </para>
  497. <programlisting language="php"><![CDATA[
  498. I have seen 'The Jerk' 1 time(s).
  499. I have seen 'The Jerk' 2 time(s).
  500. I have seen 'The Jerk' 3 time(s).
  501. ]]></programlisting>
  502. <para>
  503. Sometimes you will need access to the calling <classname>Zend_View</classname>
  504. object -- for instance, if you need to use the registered encoding,
  505. or want to render another view script as part of your helper. To get
  506. access to the view object, your helper class should have a
  507. <methodname>setView($view)</methodname> method, like the following:
  508. </para>
  509. <programlisting language="php"><![CDATA[
  510. class My_View_Helper_ScriptPath
  511. {
  512. public $view;
  513. public function setView(Zend_View_Interface $view)
  514. {
  515. $this->view = $view;
  516. }
  517. public function scriptPath($script)
  518. {
  519. return $this->view->getScriptPath($script);
  520. }
  521. }
  522. ]]></programlisting>
  523. <para>
  524. If your helper class has a <methodname>setView()</methodname> method, it will be
  525. called when the helper class is first instantiated, and passed the
  526. current view object. It is up to you to persist the object in your
  527. class, as well as determine how it should be accessed.
  528. </para>
  529. <para>
  530. If you are extending <classname>Zend_View_Helper_Abstract</classname>, you do
  531. not need to define this method, as it is defined for you.
  532. </para>
  533. </sect2>
  534. <sect2 id="zend.view.helpers.registering-concrete">
  535. <title>Registering Concrete Helpers</title>
  536. <para>
  537. Sometimes it is convenient to instantiate a view helper, and then register it with the
  538. view. As of version 1.10.0, this is now possible using the
  539. <methodname>registerHelper()</methodname> method, which expects two arguments: the
  540. helper object, and the name by which it will be registered.
  541. </para>
  542. <programlisting language="php"><![CDATA[
  543. $helper = new My_Helper_Foo();
  544. // ...do some configuration or dependency injection...
  545. $view->registerHelper($helper, 'foo');
  546. ]]></programlisting>
  547. <para>
  548. If the helper has a <methodname>setView()</methodname> method, the view object will call
  549. this and inject itself into the helper on registration.
  550. </para>
  551. <note>
  552. <title>Helper name should match a method</title>
  553. <para>
  554. The second argument to <methodname>registerHelper()</methodname> is the name of the
  555. helper. A corresponding method name should exist in the helper; otherwise,
  556. <classname>Zend_View</classname> will call a non-existent method when invoking the
  557. helper, raising a fatal <acronym>PHP</acronym> error.
  558. </para>
  559. </note>
  560. </sect2>
  561. </sect1>
  562. <!--
  563. vim:se ts=4 sw=4 et:
  564. -->