Zend_View-Helpers.xml 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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>formImage($name, $value, $attribs)</methodname>: Creates an
  211. &lt;input type="image" /&gt; element.
  212. </para>
  213. <programlisting language="php"><![CDATA[
  214. echo $this->formImage(
  215. 'foo',
  216. 'bar',
  217. array(
  218. 'src' => 'images/button.png',
  219. 'alt' => 'Button',
  220. )
  221. );
  222. // Output: <input type="image" name="foo" id="foo" src="images/button.png" value="bar" alt="Button" />
  223. ]]></programlisting>
  224. </listitem>
  225. <listitem>
  226. <para>
  227. <methodname>formLabel($name, $value, $attribs)</methodname>: Creates a
  228. &lt;label&gt; element, setting the <property>for</property> attribute to
  229. <varname>$name</varname>, and the actual label text to
  230. <varname>$value</varname>. If <emphasis>disable</emphasis> is passed in
  231. <property>attribs</property>, nothing will be returned.
  232. </para>
  233. </listitem>
  234. <listitem>
  235. <para>
  236. <methodname>formMultiCheckbox($name, $value, $attribs, $options,
  237. $listsep)</methodname>: Creates a list of checkboxes.
  238. <varname>$options</varname> should be an associative array, and may be
  239. arbitrarily deep. <varname>$value</varname> may be a single value or
  240. an array of selected values that match the keys in the
  241. <varname>$options</varname> array. <varname>$listsep</varname> is an
  242. <acronym>HTML</acronym> break ("&lt;br /&gt;") by default. By default, this
  243. element is treated as an array; all checkboxes share the same name, and are
  244. submitted as an array.
  245. </para>
  246. <programlisting language="php"><![CDATA[
  247. // Using list separator ($listsep):
  248. echo '<ul><li>';
  249. echo $view->formMultiCheckbox(
  250. 'foo',
  251. 2,
  252. array(
  253. 'class' => 'baz',
  254. ),
  255. array(
  256. 1 => 'One',
  257. 2 => 'Two',
  258. 3 => 'Three',
  259. ),
  260. "</li>\n<li>"
  261. );
  262. echo '</li></ul>';
  263. /*
  264. Output:
  265. <ul>
  266. <li>
  267. <label for="foo-1">
  268. <input type="checkbox" name="foo[]" id="foo-1" value="1" class="baz" />One
  269. </label>
  270. </li>
  271. <li>
  272. <label for="foo-2">
  273. <input type="checkbox" name="foo[]" id="foo-2" value="2" checked="checked" class="baz" />Two
  274. </label>
  275. </li>
  276. <li>
  277. <label for="foo-3">
  278. <input type="checkbox" name="foo[]" id="foo-3" value="3" class="baz" />Three</label>
  279. </li>
  280. </ul>
  281. */
  282. // Using options for label elements:
  283. echo $this->formMultiCheckbox(
  284. 'foo',
  285. 2,
  286. array(
  287. 'label_placement' => 'prepend',
  288. 'label_class' => 'baz',
  289. ),
  290. array(
  291. 1 => 'One',
  292. 2 => 'Two',
  293. 3 => 'Three',
  294. )
  295. );
  296. /*
  297. Output:
  298. <label class="baz" for="foo-1">
  299. One<input type="checkbox" name="foo[]" id="foo-1" value="1" />
  300. </label>
  301. <br />
  302. <label class="baz" for="foo-2">
  303. Two<input type="checkbox" name="foo[]" id="foo-2" value="2" checked="checked" />
  304. </label>
  305. <br />
  306. <label class="baz" for="foo-3">
  307. Three<input type="checkbox" name="foo[]" id="foo-3" value="3" />
  308. </label>
  309. */
  310. ]]></programlisting>
  311. </listitem>
  312. <listitem>
  313. <para>
  314. <methodname>formNote($name, $value = null)</methodname>: Creates a
  315. simple text note. (e.g. as element for headlines in a
  316. <classname>Zend_Form</classname> object)
  317. </para>
  318. <programlisting language="php"><![CDATA[
  319. echo $this->formNote(null, 'This is an example text.');
  320. // Output: This is an example text.
  321. ]]></programlisting>
  322. </listitem>
  323. <listitem>
  324. <para>
  325. <methodname>formPassword($name, $value, $attribs)</methodname>: Creates an
  326. &lt;input type="password" /&gt; element.
  327. </para>
  328. </listitem>
  329. <listitem>
  330. <para>
  331. <methodname>formRadio($name, $value, $attribs, $options, $listsep)</methodname>:
  332. Creates a series of &lt;input type="radio" /&gt; elements, one
  333. for each of the $options elements. In the $options array, the
  334. element key is the radio value, and the element value is the
  335. radio label. The $value radio will be preselected for you.
  336. </para>
  337. <programlisting language="php"><![CDATA[
  338. // Using list separator ($listsep)
  339. echo '<ul><li>';
  340. echo $this->formRadio(
  341. 'foo',
  342. 2,
  343. array(
  344. 'class' => 'baz',
  345. ),
  346. array(
  347. 1 => 'One',
  348. 2 => 'Two',
  349. 3 => 'Three',
  350. ),
  351. "</li>\n<li>"
  352. );
  353. echo '</li></ul>';
  354. /*
  355. Output:
  356. <ul>
  357. <li>
  358. <label for="foo-1">
  359. <input type="radio" name="foo" id="foo-1" value="1" class="baz" />One
  360. </label>
  361. </li>
  362. <li>
  363. <label for="foo-2">
  364. <input type="radio" name="foo" id="foo-2" value="2" checked="checked" class="baz" />Two
  365. </label>
  366. </li>
  367. <li>
  368. <label for="foo-3">
  369. <input type="radio" name="foo" id="foo-3" value="3" class="baz" />Three
  370. </label>
  371. </li>
  372. </ul>
  373. */
  374. // Using options for label elements:
  375. echo $this->formRadio(
  376. 'foo',
  377. 2,
  378. array(
  379. 'label_placement' => 'prepend',
  380. 'label_class' => 'baz',
  381. ),
  382. array(
  383. 1 => 'One',
  384. 2 => 'Two',
  385. 3 => 'Three',
  386. )
  387. );
  388. /*
  389. Output:
  390. <label class="baz" for="foo-1">
  391. One<input type="radio" name="foo" id="foo-1" value="1" />
  392. </label>
  393. <br />
  394. <label class="baz" for="foo-2">
  395. Two<input type="radio" name="foo" id="foo-2" value="2" checked="checked" />
  396. </label>
  397. <br />
  398. <label class="baz" for="foo-3">
  399. Three<input type="radio" name="foo" id="foo-3" value="3" />
  400. </label>
  401. */
  402. ]]></programlisting>
  403. </listitem>
  404. <listitem>
  405. <para>
  406. <methodname>formReset($name, $value, $attribs)</methodname>: Creates an
  407. &lt;input type="reset" /&gt; element.
  408. </para>
  409. </listitem>
  410. <listitem>
  411. <para>
  412. <methodname>formSelect($name, $value, $attribs, $options)</methodname>:
  413. Creates a &lt;select&gt;...&lt;/select&gt; block, with one
  414. &lt;option&gt;one for each of the $options elements. In the
  415. $options array, the element key is the option value, and the
  416. element value is the option label. The $value option(s) will be
  417. preselected for you.
  418. </para>
  419. <programlisting language="php"><![CDATA[
  420. // Using option groups:
  421. echo $view->formSelect(
  422. 'foo',
  423. 2,
  424. array(
  425. 'class' => 'baz',
  426. ),
  427. array(
  428. 1 => 'One',
  429. 'Two' => array(
  430. '2.1' => 'One',
  431. '2.2' => 'Two',
  432. '2.3' => 'Three',
  433. ),
  434. 3 => 'Three',
  435. )
  436. );
  437. /*
  438. Output:
  439. <select name="foo" id="foo" class="baz">
  440. <option value="1" label="One">One</option>
  441. <optgroup id="foo-optgroup-Two" label="Two">
  442. <option value="2.1" label="One">One</option>
  443. <option value="2.2" label="Two">Two</option>
  444. <option value="2.3" label="Three">Three</option>
  445. </optgroup>
  446. <option value="3" label="Three">Three</option>
  447. </select>
  448. */
  449. // First example with 'multiple' option:
  450. echo $this->formSelect(
  451. 'foo[]',
  452. 2,
  453. null,
  454. array(
  455. 1 => 'One',
  456. 2 => 'Two',
  457. 3 => 'Three',
  458. )
  459. );
  460. /*
  461. Output:
  462. <select name="foo[]" id="foo" multiple="multiple">
  463. <option value="1" label="One">One</option>
  464. <option value="2" label="Two" selected="selected">Two</option>
  465. <option value="3" label="Three">Three</option>
  466. </select>
  467. */
  468. // Second example with 'multiple' option:
  469. echo $this->formSelect(
  470. 'foo',
  471. array(
  472. 1,
  473. 2,
  474. ),
  475. array(
  476. 'multiple' => true,
  477. ),
  478. array(
  479. 1 => 'One',
  480. 2 => 'Two',
  481. 3 => 'Three',
  482. )
  483. );
  484. /*
  485. Output:
  486. <select name="foo[]" id="foo" multiple="multiple">
  487. <option value="1" label="One" selected="selected">One</option>
  488. <option value="2" label="Two" selected="selected">Two</option>
  489. <option value="3" label="Three">Three</option>
  490. </select>
  491. */
  492. ]]></programlisting>
  493. </listitem>
  494. <listitem>
  495. <para>
  496. <methodname>formSubmit($name, $value, $attribs)</methodname>: Creates an
  497. &lt;input type="submit" /&gt; element.
  498. </para>
  499. </listitem>
  500. <listitem>
  501. <para>
  502. <methodname>formText($name, $value, $attribs)</methodname>: Creates an
  503. &lt;input type="text" /&gt; element.
  504. </para>
  505. </listitem>
  506. <listitem>
  507. <para>
  508. <methodname>formTextarea($name, $value, $attribs)</methodname>: Creates a
  509. &lt;textarea&gt;...&lt;/textarea&gt; block.
  510. </para>
  511. </listitem>
  512. <listitem>
  513. <para>
  514. <methodname>url($urlOptions, $name, $reset, $encode)</methodname>: Creates a
  515. <acronym>URL</acronym> string based on a named route.
  516. <varname>$urlOptions</varname> should be an associative array of key/value pairs
  517. used by the particular route.
  518. </para>
  519. <programlisting language="php"><![CDATA[
  520. // Using without options: (current request is: user/id/1)
  521. echo $this->url();
  522. // Output: user/info/id/1
  523. // Set URL options:
  524. echo $this->url(
  525. array('controller' => 'user', 'action' => 'info', 'username' => 'foobar')
  526. );
  527. // Output: user/info/username/foobar
  528. // Using a route:
  529. $router->addRoute(
  530. 'user',
  531. new Zend_Controller_Router_Route(
  532. 'user/:username',
  533. array(
  534. 'controller' => 'user',
  535. 'action' => 'info',
  536. )
  537. )
  538. );
  539. echo $this->url(array('name' => 'foobar'), 'user');
  540. // Output: user/foobar
  541. // Using reset: (current request is: user/id/1)
  542. echo $this->url(array('controller' => 'user', 'action' => 'info'), null, false);
  543. // Output: user/info/id/1
  544. echo $this->url(array('controller' => 'user', 'action' => 'info'), null, true);
  545. // Output: user/info
  546. // Using encode:
  547. echo $this->url(
  548. array('controller' => 'user', 'action' => 'info', 'username' => 'John Doe'), null, true, false
  549. );
  550. // Output: user/info/username/John Doe
  551. echo $this->url(
  552. array('controller' => 'user', 'action' => 'info', 'username' => 'John Doe'), null, true, false
  553. );
  554. // Output: user/info/username/John+Doe
  555. ]]></programlisting>
  556. </listitem>
  557. <listitem>
  558. <para>
  559. <methodname>serverUrl($requestUri = null)</methodname>: Helper
  560. for returning the current server URL (optionally with request URI).
  561. </para>
  562. <programlisting language="php"><![CDATA[
  563. // Current server URL in the example is: http://www.example.com/foo.html
  564. echo $this->serverUrl();
  565. // Output: http://www.example.com
  566. echo $this->serverUrl(true);
  567. // Output: http://www.example.com/foo.html
  568. echo $this->serverUrl('/foo/bar');
  569. // Output: http://www.example.com/foo/bar
  570. echo $this->serverUrl()->getHost();
  571. // Output: www.example.com
  572. echo $this->serverUrl()->getScheme();
  573. // Output: http
  574. $this->serverUrl()->setHost('www.foo.com');
  575. $this->serverUrl()->setScheme('https');
  576. echo $this->serverUrl();
  577. // Output: https://www.foo.com
  578. ]]></programlisting>
  579. </listitem>
  580. <listitem>
  581. <para>
  582. <methodname>htmlList($items, $ordered, $attribs, $escape)</methodname>:
  583. generates unordered and ordered lists based on the <varname>$items</varname>
  584. passed to it. If <varname>$items</varname> is a multidimensional
  585. array, a nested list will be built. If the <varname>$escape</varname>
  586. flag is <constant>TRUE</constant> (default), individual items will be escaped
  587. using the view objects registered escaping mechanisms; pass a
  588. <constant>FALSE</constant> value if you want to allow markup in your lists.
  589. </para>
  590. </listitem>
  591. </itemizedlist>
  592. <para>
  593. Using these in your view scripts is very easy, here is an example.
  594. Note that you all you need to do is call them; they will load
  595. and instantiate themselves as they are needed.
  596. </para>
  597. <programlisting language="php"><![CDATA[
  598. // inside your view script, $this refers to the Zend_View instance.
  599. //
  600. // say that you have already assigned a series of select options under
  601. // the name $countries as array('us' => 'United States', 'il' =>
  602. // 'Israel', 'de' => 'Germany').
  603. ?>
  604. <form action="action.php" method="post">
  605. <p><label>Your Email:
  606. <?php echo $this->formText('email', 'you@example.com', array('size' => 32)) ?>
  607. </label></p>
  608. <p><label>Your Country:
  609. <?php echo $this->formSelect('country', 'us', null, $this->countries) ?>
  610. </label></p>
  611. <p><label>Would you like to opt in?
  612. <?php echo $this->formCheckbox('opt_in', 'yes', null, array('yes', 'no')) ?>
  613. </label></p>
  614. </form>
  615. ]]></programlisting>
  616. <para>
  617. The resulting output from the view script will look something like this:
  618. </para>
  619. <programlisting language="php"><![CDATA[
  620. <form action="action.php" method="post">
  621. <p><label>Your Email:
  622. <input type="text" name="email" value="you@example.com" size="32" />
  623. </label></p>
  624. <p><label>Your Country:
  625. <select name="country">
  626. <option value="us" selected="selected">United States</option>
  627. <option value="il">Israel</option>
  628. <option value="de">Germany</option>
  629. </select>
  630. </label></p>
  631. <p><label>Would you like to opt in?
  632. <input type="hidden" name="opt_in" value="no" />
  633. <input type="checkbox" name="opt_in" value="yes" checked="checked" />
  634. </label></p>
  635. </form>
  636. ]]></programlisting>
  637. <xi:include href="Zend_View-Helpers-Action.xml" />
  638. <xi:include href="Zend_View-Helpers-BaseUrl.xml" />
  639. <xi:include href="Zend_View-Helpers-Currency.xml" />
  640. <xi:include href="Zend_View-Helpers-Cycle.xml" />
  641. <xi:include href="Zend_View-Helpers-Partial.xml" />
  642. <xi:include href="Zend_View-Helpers-Placeholder.xml" />
  643. <xi:include href="Zend_View-Helpers-Doctype.xml" />
  644. <xi:include href="Zend_View-Helpers-Gravatar.xml" />
  645. <xi:include href="Zend_View-Helpers-HeadLink.xml" />
  646. <xi:include href="Zend_View-Helpers-HeadMeta.xml" />
  647. <xi:include href="Zend_View-Helpers-HeadScript.xml" />
  648. <xi:include href="Zend_View-Helpers-HeadStyle.xml" />
  649. <xi:include href="Zend_View-Helpers-HeadTitle.xml" />
  650. <xi:include href="Zend_View-Helpers-HtmlObject.xml" />
  651. <xi:include href="Zend_View-Helpers-InlineScript.xml" />
  652. <xi:include href="Zend_View-Helpers-RenderToPlaceholder.xml" />
  653. <xi:include href="Zend_View-Helpers-Json.xml" />
  654. <xi:include href="Zend_View-Helpers-Navigation.xml" />
  655. <xi:include href="Zend_View-Helpers-Translate.xml" />
  656. <xi:include href="Zend_View-Helpers-UserAgent.xml" />
  657. </sect2>
  658. <sect2 id="zend.view.helpers.paths">
  659. <title>Helper Paths</title>
  660. <para>
  661. As with view scripts, your controller can specify a stack of paths
  662. for <classname>Zend_View</classname> to search for helper classes. By default,
  663. <classname>Zend_View</classname> looks in "Zend/View/Helper/*" for helper
  664. classes. You can tell <classname>Zend_View</classname> to look in other
  665. locations using the <methodname>setHelperPath()</methodname> and
  666. <methodname>addHelperPath()</methodname> methods. Additionally, you can
  667. indicate a class prefix to use for helpers in the path provided, to
  668. allow namespacing your helper classes. By default, if no class
  669. prefix is provided, 'Zend_View_Helper_' is assumed.
  670. </para>
  671. <programlisting language="php"><![CDATA[
  672. $view = new Zend_View();
  673. // Set path to /path/to/more/helpers, with prefix 'My_View_Helper'
  674. $view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');
  675. ]]></programlisting>
  676. <para>
  677. In fact, you can "stack" paths using the
  678. <methodname>addHelperPath()</methodname> method. As you add paths to the stack,
  679. <classname>Zend_View</classname> will look at the most-recently-added path for
  680. the requested helper class. This allows you to add to (or even
  681. override) the initial distribution of helpers with your own custom
  682. helpers.
  683. </para>
  684. <programlisting language="php"><![CDATA[
  685. $view = new Zend_View();
  686. // Add /path/to/some/helpers with class prefix 'My_View_Helper'
  687. $view->addHelperPath('/path/to/some/helpers', 'My_View_Helper');
  688. // Add /other/path/to/helpers with class prefix 'Your_View_Helper'
  689. $view->addHelperPath('/other/path/to/helpers', 'Your_View_Helper');
  690. // now when you call $this->helperName(), Zend_View will look first for
  691. // "/path/to/some/helpers/HelperName" using class name
  692. // "Your_View_Helper_HelperName", then for
  693. // "/other/path/to/helpers/HelperName.php" using class name
  694. // "My_View_Helper_HelperName", and finally for
  695. // "Zend/View/Helper/HelperName.php" using class name
  696. // "Zend_View_Helper_HelperName".
  697. ]]></programlisting>
  698. </sect2>
  699. <sect2 id="zend.view.helpers.custom">
  700. <title>Writing Custom Helpers</title>
  701. <para>
  702. Writing custom helpers is easy; just follow these rules:
  703. </para>
  704. <itemizedlist>
  705. <listitem>
  706. <para>
  707. While not strictly necessary, we recommend either implementing
  708. <classname>Zend_View_Helper_Interface</classname> or extending
  709. <classname>Zend_View_Helper_Abstract</classname> when creating your
  710. helpers. Introduced in 1.6.0, these simply define a
  711. <methodname>setView()</methodname> method; however, in upcoming releases, we
  712. plan to implement a strategy pattern that will simplify much of
  713. the naming schema detailed below. Building off these now will
  714. help you future-proof your code.
  715. </para>
  716. </listitem>
  717. <listitem>
  718. <para>
  719. The class name must, at the very minimum, end with the helper
  720. name itself, using MixedCaps. E.g., if you were writing a
  721. helper called "specialPurpose", the class name would minimally
  722. need to be "SpecialPurpose". You may, and should, give the class
  723. name a prefix, and it is recommended that you use 'View_Helper'
  724. as part of that prefix: "My_View_Helper_SpecialPurpose". (You
  725. will need to pass in the prefix, with or without the trailing
  726. underscore, to <methodname>addHelperPath()</methodname> or
  727. <methodname>setHelperPath()</methodname>).
  728. </para>
  729. </listitem>
  730. <listitem>
  731. <para>
  732. The class must have a public method that matches the
  733. helper name; this is the method that will be called when
  734. your template calls "$this->specialPurpose()". In our
  735. "specialPurpose" helper example, the required method
  736. declaration would be "public function specialPurpose()".
  737. </para>
  738. </listitem>
  739. <listitem>
  740. <para>
  741. In general, the class should not echo or print or otherwise
  742. generate output. Instead, it should return values to be
  743. printed or echoed. The returned values should be escaped
  744. appropriately.
  745. </para>
  746. </listitem>
  747. <listitem>
  748. <para>
  749. The class must be in a file named after the helper class. Again
  750. using our "specialPurpose" helper example, the file has to be
  751. named "SpecialPurpose.php".
  752. </para>
  753. </listitem>
  754. </itemizedlist>
  755. <para>
  756. Place the helper class file somewhere in your helper path stack, and
  757. <classname>Zend_View</classname> will automatically load, instantiate,
  758. persist, and execute it for you.
  759. </para>
  760. <para>
  761. Here is an example of our <classname>SpecialPurpose</classname> helper code:
  762. </para>
  763. <programlisting language="php"><![CDATA[
  764. class My_View_Helper_SpecialPurpose extends Zend_View_Helper_Abstract
  765. {
  766. protected $_count = 0;
  767. public function specialPurpose()
  768. {
  769. $this->_count++;
  770. $output = "I have seen 'The Jerk' {$this->_count} time(s).";
  771. return htmlspecialchars($output);
  772. }
  773. }
  774. ]]></programlisting>
  775. <para>
  776. Then in a view script, you can call the <classname>SpecialPurpose</classname>
  777. helper as many times as you like; it will be instantiated once, and
  778. then it persists for the life of that <classname>Zend_View</classname>
  779. instance.
  780. </para>
  781. <programlisting language="php"><![CDATA[
  782. // remember, in a view script, $this refers to the Zend_View instance.
  783. echo $this->specialPurpose();
  784. echo $this->specialPurpose();
  785. echo $this->specialPurpose();
  786. ]]></programlisting>
  787. <para>
  788. The output would look something like this:
  789. </para>
  790. <programlisting language="php"><![CDATA[
  791. I have seen 'The Jerk' 1 time(s).
  792. I have seen 'The Jerk' 2 time(s).
  793. I have seen 'The Jerk' 3 time(s).
  794. ]]></programlisting>
  795. <para>
  796. Sometimes you will need access to the calling <classname>Zend_View</classname>
  797. object -- for instance, if you need to use the registered encoding,
  798. or want to render another view script as part of your helper. To get
  799. access to the view object, your helper class should have a
  800. <methodname>setView($view)</methodname> method, like the following:
  801. </para>
  802. <programlisting language="php"><![CDATA[
  803. class My_View_Helper_ScriptPath
  804. {
  805. public $view;
  806. public function setView(Zend_View_Interface $view)
  807. {
  808. $this->view = $view;
  809. }
  810. public function scriptPath($script)
  811. {
  812. return $this->view->getScriptPath($script);
  813. }
  814. }
  815. ]]></programlisting>
  816. <para>
  817. If your helper class has a <methodname>setView()</methodname> method, it will be
  818. called when the helper class is first instantiated, and passed the
  819. current view object. It is up to you to persist the object in your
  820. class, as well as determine how it should be accessed.
  821. </para>
  822. <para>
  823. If you are extending <classname>Zend_View_Helper_Abstract</classname>, you do
  824. not need to define this method, as it is defined for you.
  825. </para>
  826. </sect2>
  827. <sect2 id="zend.view.helpers.registering-concrete">
  828. <title>Registering Concrete Helpers</title>
  829. <para>
  830. Sometimes it is convenient to instantiate a view helper, and then register it with the
  831. view. As of version 1.10.0, this is now possible using the
  832. <methodname>registerHelper()</methodname> method, which expects two arguments: the
  833. helper object, and the name by which it will be registered.
  834. </para>
  835. <programlisting language="php"><![CDATA[
  836. $helper = new My_Helper_Foo();
  837. // ...do some configuration or dependency injection...
  838. $view->registerHelper($helper, 'foo');
  839. ]]></programlisting>
  840. <para>
  841. If the helper has a <methodname>setView()</methodname> method, the view object will call
  842. this and inject itself into the helper on registration.
  843. </para>
  844. <note>
  845. <title>Helper name should match a method</title>
  846. <para>
  847. The second argument to <methodname>registerHelper()</methodname> is the name of the
  848. helper. A corresponding method name should exist in the helper; otherwise,
  849. <classname>Zend_View</classname> will call a non-existent method when invoking the
  850. helper, raising a fatal <acronym>PHP</acronym> error.
  851. </para>
  852. </note>
  853. </sect2>
  854. </sect1>
  855. <!--
  856. vim:se ts=4 sw=4 et:
  857. -->