Zend_View-Helpers-Placeholder.xml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <code>Placeholder</code> 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 <code>Placeholder</code> 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 <code>Placeholder</code> view helper uses containers that extend
  38. <code>ArrayObject</code>, 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><para>
  44. <code>setPrefix($prefix)</code> sets text with which to
  45. prefix the content. Use <code>getPrefix()</code> at any time
  46. to determine what the current setting is.
  47. </para></listitem>
  48. <listitem><para>
  49. <code>setPostfix($prefix)</code> sets text with which to
  50. append the content. Use <code>getPostfix()</code> at any time
  51. to determine what the current setting is.
  52. </para></listitem>
  53. <listitem><para>
  54. <code>setSeparator($prefix)</code> sets text with which to
  55. separate aggregated content. Use <code>getSeparator()</code>
  56. at any time to determine what the current setting is.
  57. </para></listitem>
  58. <listitem><para>
  59. <code>setIndent($prefix)</code> can be used to set an
  60. indentation value for content. If an integer is passed,
  61. that number of spaces will be used; if a string is passed,
  62. the string will be used. Use <code>getIndent()</code>
  63. at any time to determine what the current setting is.
  64. </para></listitem>
  65. </itemizedlist>
  66. <programlisting language="php"><![CDATA[
  67. <!-- first view script -->
  68. <?php $this->placeholder('foo')->exchangeArray($this->data) ?>
  69. ]]></programlisting>
  70. <programlisting language="php"><![CDATA[
  71. <!-- later view script -->
  72. <?php
  73. $this->placeholder('foo')->setPrefix("<ul>\n <li>")
  74. ->setSeparator("</li><li>\n")
  75. ->setIndent(4)
  76. ->setPostfix("</li></ul>\n");
  77. ?>
  78. <?php
  79. echo $this->placeholder('foo');
  80. // outputs as unordered list with pretty indentation
  81. ?>
  82. ]]></programlisting>
  83. <para>
  84. Because the <code>Placeholder</code> container objects extend
  85. <code>ArrayObject</code>, you can also assign content to a specific
  86. key in the container easily, instead of simply pushing it into the
  87. container. Keys may be accessed either as object properties or as
  88. array keys.
  89. </para>
  90. <programlisting language="php"><![CDATA[
  91. <?php $this->placeholder('foo')->bar = $this->data ?>
  92. <?php echo $this->placeholder('foo')->bar ?>
  93. <?php
  94. $foo = $this->placeholder('foo');
  95. echo $foo['bar'];
  96. ?>
  97. ]]></programlisting>
  98. </example>
  99. <example id="zend.view.helpers.initial.placeholder.capture">
  100. <title>Using Placeholders to Capture Content</title>
  101. <para>
  102. Occasionally you may have content for a placeholder in a view script
  103. that is easiest to template; the <code>Placeholder</code> view
  104. helper allows you to capture arbitrary content for later rendering
  105. using the following API.
  106. </para>
  107. <itemizedlist>
  108. <listitem>
  109. <para>
  110. <code>captureStart($type, $key)</code> begins capturing
  111. content.
  112. </para>
  113. <para>
  114. <code>$type</code> should be one of the
  115. <code>Placeholder</code> constants <code>APPEND</code> or
  116. <code>SET</code>. If <code>APPEND</code>, captured content
  117. is appended to the list of current content in the
  118. placeholder; if <code>SET</code>, captured content is used
  119. as the sole value of the placeholder (potentially replacing
  120. any previous content). By default, <code>$type</code> is
  121. <code>APPEND</code>.
  122. </para>
  123. <para>
  124. <code>$key</code> can be used to specify a specific key in
  125. the placeholder container to which you want content
  126. captured.
  127. </para>
  128. <para>
  129. <code>captureStart()</code> locks capturing until
  130. <code>captureEnd()</code> is called; you cannot nest
  131. capturing with the same placeholder container. Doing so will
  132. raise an exception.
  133. </para>
  134. </listitem>
  135. <listitem><para>
  136. <code>captureEnd()</code> stops capturing content, and
  137. places it in the container object according to how
  138. <code>captureStart()</code> was called.
  139. </para></listitem>
  140. </itemizedlist>
  141. <programlisting language="php"><![CDATA[
  142. <!-- Default capture: append -->
  143. <?php $this->placeholder('foo')->captureStart();
  144. foreach ($this->data as $datum): ?>
  145. <div class="foo">
  146. <h2><?php echo $datum->title ?></h2>
  147. <p><?php echo $datum->content ?></p>
  148. </div>
  149. <?php endforeach; ?>
  150. <?php $this->placeholder('foo')->captureEnd() ?>
  151. <?php echo $this->placeholder('foo') ?>
  152. ]]></programlisting>
  153. <programlisting language="php"><![CDATA[
  154. <!-- Capture to key -->
  155. <?php $this->placeholder('foo')->captureStart('SET', 'data');
  156. foreach ($this->data as $datum): ?>
  157. <div class="foo">
  158. <h2><?php echo $datum->title ?></h2>
  159. <p><?php echo $datum->content ?></p>
  160. </div>
  161. <?php endforeach; ?>
  162. <?php $this->placeholder('foo')->captureEnd() ?>
  163. <?php echo $this->placeholder('foo')->data ?>
  164. ]]></programlisting>
  165. </example>
  166. <sect4 id="zend.view.helpers.initial.placeholder.implementations">
  167. <title>Concrete Placeholder Implementations</title>
  168. <para>
  169. Zend Framework ships with a number of "concrete" placeholder
  170. implementations. These are for commonly used placeholders: doctype,
  171. page title, and various &lt;head&gt; elements. In all cases, calling
  172. the placeholder with no arguments returns the element itself.
  173. </para>
  174. <para>
  175. Documentation for each element is covered separately, as linked
  176. below:
  177. </para>
  178. <itemizedlist>
  179. <listitem><para>
  180. <link linkend="zend.view.helpers.initial.doctype">Doctype</link>
  181. </para></listitem>
  182. <listitem><para>
  183. <link linkend="zend.view.helpers.initial.headlink">HeadLink</link>
  184. </para></listitem>
  185. <listitem><para>
  186. <link linkend="zend.view.helpers.initial.headmeta">HeadMeta</link>
  187. </para></listitem>
  188. <listitem><para>
  189. <link linkend="zend.view.helpers.initial.headscript">HeadScript</link>
  190. </para></listitem>
  191. <listitem><para>
  192. <link linkend="zend.view.helpers.initial.headstyle">HeadStyle</link>
  193. </para></listitem>
  194. <listitem><para>
  195. <link linkend="zend.view.helpers.initial.headtitle">HeadTitle</link>
  196. </para></listitem>
  197. <listitem><para>
  198. <link linkend="zend.view.helpers.initial.inlinescript">InlineScript</link>
  199. </para></listitem>
  200. </itemizedlist>
  201. </sect4>
  202. </sect3>
  203. <!--
  204. vim:se ts=4 sw=4 et:
  205. -->