view-placeholders-basics.xml 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19777 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.view.placeholders.basics">
  5. <title>Basic Placeholder Usage</title>
  6. <para>
  7. Zend Framework defines a generic <methodname>placeholder()</methodname> view helper that you
  8. may use for as many custom placeholders you need. It also provides a variety of specific
  9. placeholder implementations for often-needed functionality, such as specifying the
  10. <acronym>DocType</acronym> declaration, document title, and more.
  11. </para>
  12. <para>
  13. All placeholders operate in roughly the same way. They are containers, and thus allow you to
  14. operate on them as collections. With them you can:
  15. </para>
  16. <itemizedlist>
  17. <listitem>
  18. <para>
  19. <emphasis>Append</emphasis> or <emphasis>prepend</emphasis> items to the collection.
  20. </para>
  21. </listitem>
  22. <listitem>
  23. <para>
  24. <emphasis>Replace</emphasis> the entire collection with a single value.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. Specify a string with which to <emphasis>prepend output</emphasis> of the collection
  30. when rendering.
  31. </para>
  32. </listitem>
  33. <listitem>
  34. <para>
  35. Specify a string with which to <emphasis>append output</emphasis> of the collection
  36. when rendering.
  37. </para>
  38. </listitem>
  39. <listitem>
  40. <para>
  41. Specify a string with which to <emphasis>separate items</emphasis> of the collection
  42. when rendering.
  43. </para>
  44. </listitem>
  45. <listitem>
  46. <para>
  47. <emphasis>Capture content</emphasis> into the collection.
  48. </para>
  49. </listitem>
  50. <listitem>
  51. <para>
  52. <emphasis>Render</emphasis> the aggregated content.
  53. </para>
  54. </listitem>
  55. </itemizedlist>
  56. <para>
  57. Typically, you will call the helper with no arguments, which will return a container on
  58. which you may operate. You will then either echo this container to render it, or call
  59. methods on it to configure or populate it. If the container is empty, rendering it will
  60. simply return an empty string; otherwise, the content will be aggregated according to the
  61. rules by which you configure it.
  62. </para>
  63. <para>
  64. As an example, let's create a sidebar that consists of a number of "blocks" of content.
  65. You'll likely know up-front the structure of each block; let's assume for this example that
  66. it might look like this:
  67. </para>
  68. <programlisting language="html"><![CDATA[
  69. <div class="sidebar">
  70. <div class="block">
  71. <p>
  72. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus consectetur aliquet
  73. odio ac consectetur. Nulla quis eleifend tortor. Pellentesque varius, odio quis bibendum
  74. consequat, diam lectus porttitor quam, et aliquet mauris orci eu augue.
  75. </p>
  76. </div>
  77. <div class="block">
  78. <ul>
  79. <li><a href="/some/target">Link</a></li>
  80. <li><a href="/some/target">Link</a></li>
  81. </ul>
  82. </div>
  83. </div>
  84. ]]></programlisting>
  85. <para>
  86. The content will vary based on the controller and action, but the structure will be the
  87. same. Let's first setup the sidebar in a resource method of our bootstrap:
  88. </para>
  89. <programlisting language="php"><![CDATA[
  90. class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
  91. {
  92. // ...
  93. protected function _initSidebar()
  94. {
  95. $this->bootstrap('View');
  96. $view = $this->getResource('View');
  97. $view->placeholder('sidebar')
  98. // "prefix" -> markup to emit once, before all items in collection
  99. ->setPrefix("<div class=\"sidebar\">\n <div class=\"block\">\n")
  100. // "separator" -> markup to emit between items in a collection
  101. ->setSeparator("</div>\n <div class=\"block\">\n")
  102. // "postfix" -> markup to emit once, after all items in a collection
  103. ->setPostfix("</div>\n</div>");
  104. }
  105. // ...
  106. }
  107. ]]></programlisting>
  108. <para>
  109. The above defines a placeholder, "sidebar", that has no items. It configures the basic
  110. markup structure of that placeholder, however, per our requirements.
  111. </para>
  112. <para>
  113. Now, let's assume for the "user" controller that for all actions we'll want a block at the
  114. top containing some information. We could accomplish this in two ways: (a) we could add the
  115. content to the placeholder directly in the controller's
  116. <methodname>preDispatch()</methodname> method, or (b) we could render a view script from
  117. within the <methodname>preDispatch()</methodname> method. We'll use (b), as it follows a
  118. more proper separation of concerns (leaving view-related logic and functionality within a
  119. view script).
  120. </para>
  121. <para>
  122. We'll name the view script "user/_sidebar.phtml", and populate it as follows:
  123. </para>
  124. <programlisting language="php"><![CDATA[
  125. <?php $this->placeholder('sidebar')->captureStart() ?>
  126. <h4>User Administration</h4>
  127. <ul>
  128. <li><a href="<?php $this->url(array('action' => 'list')) ?>">
  129. List</a></li>
  130. <li><a href="<?php $this->url(array('action' => 'create')) ?>">
  131. Create</a></a></li>
  132. </ul>
  133. <?php $this->placeholder('sidebar')->captureEnd() ?>
  134. ]]></programlisting>
  135. <para>
  136. The above example makes use of the content capturing feature of placeholders. By default,
  137. content is appended as a new item in the container, allowing us to aggregate content. This
  138. example makes use of view helpers and static HTML in order to generate markup, and the
  139. content is then captured and appended into the placeholder itself.
  140. </para>
  141. <para>
  142. To invoke the above view script, we would write the following in our
  143. <methodname>preDispatch()</methodname> method:
  144. </para>
  145. <programlisting language="php"><![CDATA[
  146. class UserController extends Zend_Controller_Action
  147. {
  148. // ...
  149. public function preDispatch()
  150. {
  151. // ...
  152. $this->view->render('user/_sidebar.phtml');
  153. // ...
  154. }
  155. // ...
  156. }
  157. ]]></programlisting>
  158. <para>
  159. Note that we're not capturing the rendered value; there's no need, as the entierty of that
  160. view is being captured into a placeholder.
  161. </para>
  162. <para>
  163. Now, let's assume our "view" action in that same controller needs to present some
  164. information. Within the "user/view.phtml" view script, we might have the following snippet
  165. of content:
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. $this->placeholder('sidebar')
  169. ->append('<p>User: ' . $this->escape($this->username) . '</p>');
  170. ]]></programlisting>
  171. <para>
  172. This example makes use of the <methodname>append()</methodname> method, and passes it some
  173. simple markup to aggregate.
  174. </para>
  175. <para>
  176. Finally, let's modify our layout view script, and have it render the placeholder.
  177. </para>
  178. <programlisting language="php"><![CDATA[
  179. <html>
  180. <head>
  181. <title>My Site</title>
  182. </head>
  183. <body>
  184. <div class="content">
  185. <?php echo $this->layout()->content ?>
  186. </div>
  187. <?php echo $this->placeholder('sidebar') ?>
  188. </body>
  189. </html>
  190. ]]></programlisting>
  191. <para>
  192. For controllers and actions that do not populate the "sidebar" placeholder, no content will
  193. be rendered; for those that do, however, echoing the placeholder will render the content
  194. according to the rules we created in our bootstrap, and the content we aggregated throughout
  195. the application. In the case of the "/user/view" action, and assuming a username of
  196. "matthew", we would get content for the sidebar as follows (formatted for readability):
  197. </para>
  198. <programlisting language="html"><![CDATA[
  199. <div class="sidebar">
  200. <div class="block">
  201. <h4>User Administration</h4>
  202. <ul>
  203. <li><a href="/user/list">List</a></li>
  204. <li><a href="/user/create">Create</a></a></li>
  205. </ul>
  206. </div>
  207. <div class="block">
  208. <p>User: matthew</p>
  209. </div>
  210. </div>
  211. ]]></programlisting>
  212. <para>
  213. There are a large number of things you can do by combining placeholders and layout scripts;
  214. experiment with them, and read the <link
  215. linkend="zend.view.helpers.initial.placeholder">relevant manual sections</link> for more
  216. information.
  217. </para>
  218. </sect1>