Zend_View-Helpers-Placeholder.xml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.view.helpers.initial.placeholder">
  4. <title>Placeholder Helper</title>
  5. <para>
  6. The <classname>Placeholder</classname> view helper is used to persist content
  7. between view scripts and view instances. It also offers some useful
  8. features such as aggregating content, capturing view script content
  9. for later use, and adding pre- and post-text to content (and custom
  10. separators for aggregated content).
  11. </para>
  12. <example id="zend.view.helpers.initial.placeholder.usage">
  13. <title>Basic Usage of Placeholders</title>
  14. <para>
  15. Basic usage of placeholders is to persist view data. Each invocation
  16. of the <classname>Placeholder</classname> helper expects a placeholder name;
  17. the helper then returns a placeholder container object that you can
  18. either manipulate or simply echo out.
  19. </para>
  20. <programlisting language="php"><![CDATA[
  21. <?php $this->placeholder('foo')->set("Some text for later") ?>
  22. <?php
  23. echo $this->placeholder('foo');
  24. // outputs "Some text for later"
  25. ?>
  26. ]]></programlisting>
  27. </example>
  28. <example id="zend.view.helpers.initial.placeholder.aggregation">
  29. <title>Using Placeholders to Aggregate Content</title>
  30. <para>
  31. Aggregating content via placeholders can be useful at times as well.
  32. For instance, your view script may have a variable array from which
  33. you wish to retrieve messages to display later; a later view script
  34. can then determine how those will be rendered.
  35. </para>
  36. <para>
  37. The <classname>Placeholder</classname> view helper uses containers that extend
  38. <classname>ArrayObject</classname>, providing a rich featureset for
  39. manipulating arrays. In addition, it offers a variety of methods for
  40. formatting the content stored in the container:
  41. </para>
  42. <itemizedlist>
  43. <listitem>
  44. <para>
  45. <methodname>setPrefix($prefix)</methodname> sets text with which to
  46. prefix the content. Use <methodname>getPrefix()</methodname> at any time
  47. to determine what the current setting is.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <methodname>setPostfix($prefix)</methodname> sets text with which to
  53. append the content. Use <methodname>getPostfix()</methodname> at any time
  54. to determine what the current setting is.
  55. </para>
  56. </listitem>
  57. <listitem>
  58. <para>
  59. <methodname>setSeparator($prefix)</methodname> sets text with which to
  60. separate aggregated content. Use <methodname>getSeparator()</methodname>
  61. at any time to determine what the current setting is.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. <methodname>setIndent($prefix)</methodname> can be used to set an
  67. indentation value for content. If an integer is passed,
  68. that number of spaces will be used; if a string is passed,
  69. the string will be used. Use <methodname>getIndent()</methodname>
  70. at any time to determine what the current setting is.
  71. </para>
  72. </listitem>
  73. </itemizedlist>
  74. <programlisting language="php"><![CDATA[
  75. <!-- first view script -->
  76. <?php $this->placeholder('foo')->exchangeArray($this->data) ?>
  77. ]]></programlisting>
  78. <programlisting language="php"><![CDATA[
  79. <!-- later view script -->
  80. <?php
  81. $this->placeholder('foo')->setPrefix("<ul>\n <li>")
  82. ->setSeparator("</li><li>\n")
  83. ->setIndent(4)
  84. ->setPostfix("</li></ul>\n");
  85. ?>
  86. <?php
  87. echo $this->placeholder('foo');
  88. // outputs as unordered list with pretty indentation
  89. ?>
  90. ]]></programlisting>
  91. <para>
  92. Because the <classname>Placeholder</classname> container objects extend
  93. <classname>ArrayObject</classname>, you can also assign content to a specific
  94. key in the container easily, instead of simply pushing it into the
  95. container. Keys may be accessed either as object properties or as
  96. array keys.
  97. </para>
  98. <programlisting language="php"><![CDATA[
  99. <?php $this->placeholder('foo')->bar = $this->data ?>
  100. <?php echo $this->placeholder('foo')->bar ?>
  101. <?php
  102. $foo = $this->placeholder('foo');
  103. echo $foo['bar'];
  104. ?>
  105. ]]></programlisting>
  106. </example>
  107. <example id="zend.view.helpers.initial.placeholder.capture">
  108. <title>Using Placeholders to Capture Content</title>
  109. <para>
  110. Occasionally you may have content for a placeholder in a view script
  111. that is easiest to template; the <classname>Placeholder</classname> view
  112. helper allows you to capture arbitrary content for later rendering
  113. using the following <acronym>API</acronym>.
  114. </para>
  115. <itemizedlist>
  116. <listitem>
  117. <para>
  118. <methodname>captureStart($type, $key)</methodname> begins capturing
  119. content.
  120. </para>
  121. <para>
  122. <varname>$type</varname> should be one of the
  123. <classname>Placeholder</classname> constants <constant>APPEND</constant> or
  124. <constant>SET</constant>. If <constant>APPEND</constant>, captured content
  125. is appended to the list of current content in the
  126. placeholder; if <constant>SET</constant>, captured content is used
  127. as the sole value of the placeholder (potentially replacing
  128. any previous content). By default, <varname>$type</varname> is
  129. <constant>APPEND</constant>.
  130. </para>
  131. <para>
  132. <varname>$key</varname> can be used to specify a specific key in
  133. the placeholder container to which you want content
  134. captured.
  135. </para>
  136. <para>
  137. <methodname>captureStart()</methodname> locks capturing until
  138. <methodname>captureEnd()</methodname> is called; you cannot nest
  139. capturing with the same placeholder container. Doing so will
  140. raise an exception.
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <methodname>captureEnd()</methodname> stops capturing content, and
  146. places it in the container object according to how
  147. <methodname>captureStart()</methodname> was called.
  148. </para>
  149. </listitem>
  150. </itemizedlist>
  151. <programlisting language="php"><![CDATA[
  152. <!-- Default capture: append -->
  153. <?php $this->placeholder('foo')->captureStart();
  154. foreach ($this->data as $datum): ?>
  155. <div class="foo">
  156. <h2><?php echo $datum->title ?></h2>
  157. <p><?php echo $datum->content ?></p>
  158. </div>
  159. <?php endforeach; ?>
  160. <?php $this->placeholder('foo')->captureEnd() ?>
  161. <?php echo $this->placeholder('foo') ?>
  162. ]]></programlisting>
  163. <programlisting language="php"><![CDATA[
  164. <!-- Capture to key -->
  165. <?php $this->placeholder('foo')->captureStart('SET', 'data');
  166. foreach ($this->data as $datum): ?>
  167. <div class="foo">
  168. <h2><?php echo $datum->title ?></h2>
  169. <p><?php echo $datum->content ?></p>
  170. </div>
  171. <?php endforeach; ?>
  172. <?php $this->placeholder('foo')->captureEnd() ?>
  173. <?php echo $this->placeholder('foo')->data ?>
  174. ]]></programlisting>
  175. </example>
  176. <sect4 id="zend.view.helpers.initial.placeholder.implementations">
  177. <title>Concrete Placeholder Implementations</title>
  178. <para>
  179. Zend Framework ships with a number of "concrete" placeholder
  180. implementations. These are for commonly used placeholders: doctype,
  181. page title, and various &lt;head&gt; elements. In all cases, calling
  182. the placeholder with no arguments returns the element itself.
  183. </para>
  184. <para>
  185. Documentation for each element is covered separately, as linked
  186. below:
  187. </para>
  188. <itemizedlist>
  189. <listitem>
  190. <para>
  191. <link linkend="zend.view.helpers.initial.doctype">Doctype</link>
  192. </para>
  193. </listitem>
  194. <listitem>
  195. <para>
  196. <link linkend="zend.view.helpers.initial.headlink">HeadLink</link>
  197. </para>
  198. </listitem>
  199. <listitem>
  200. <para>
  201. <link linkend="zend.view.helpers.initial.headmeta">HeadMeta</link>
  202. </para>
  203. </listitem>
  204. <listitem>
  205. <para>
  206. <link linkend="zend.view.helpers.initial.headscript">HeadScript</link>
  207. </para>
  208. </listitem>
  209. <listitem>
  210. <para>
  211. <link linkend="zend.view.helpers.initial.headstyle">HeadStyle</link>
  212. </para>
  213. </listitem>
  214. <listitem>
  215. <para>
  216. <link linkend="zend.view.helpers.initial.headtitle">HeadTitle</link>
  217. </para>
  218. </listitem>
  219. <listitem>
  220. <para>
  221. <link linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>
  222. </para>
  223. </listitem>
  224. </itemizedlist>
  225. </sect4>
  226. </sect3>
  227. <!--
  228. vim:se ts=4 sw=4 et:
  229. -->