Zend_Layout-QuickStart.xml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.layout.quickstart">
  4. <title>Zend_Layout Quick Start</title>
  5. <para>
  6. There are two primary use cases for <classname>Zend_Layout</classname>: with the
  7. Zend Framework <acronym>MVC</acronym>, and without.
  8. </para>
  9. <sect2 id="zend.layout.quickstart.layouts">
  10. <title>Layout scripts</title>
  11. <para>
  12. In both cases, however, you'll need to create a layout script. Layout scripts simply
  13. utilize <classname>Zend_View</classname> (or whatever view implementation you are
  14. using). Layout variables are registered with a <classname>Zend_Layout</classname> <link
  15. linkend="zend.view.helpers.initial.placeholder">placeholder</link>,
  16. and may be accessed via the placeholder helper or by fetching them
  17. as object properties of the layout object via the layout helper.
  18. </para>
  19. <para>
  20. As an example:
  21. </para>
  22. <programlisting language="php"><![CDATA[
  23. <!DOCTYPE html
  24. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  25. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  26. <html>
  27. <head>
  28. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  29. <title>My Site</title>
  30. </head>
  31. <body>
  32. <?php
  33. // fetch 'content' key using layout helper:
  34. echo $this->layout()->content;
  35. // fetch 'foo' key using placeholder helper:
  36. echo $this->placeholder('Zend_Layout')->foo;
  37. // fetch layout object and retrieve various keys from it:
  38. $layout = $this->layout();
  39. echo $layout->bar;
  40. echo $layout->baz;
  41. ?>
  42. </body>
  43. </html>
  44. ]]></programlisting>
  45. <para>
  46. Because <classname>Zend_Layout</classname> utilizes <classname>Zend_View</classname> for
  47. rendering, you can also use any view helpers registered, and also
  48. have access to any previously assigned view variables. Particularly
  49. useful are the various <link
  50. linkend="zend.view.helpers.initial.placeholder">placeholder
  51. helpers</link>, as they allow you to
  52. retrieve content for areas such as the &lt;head&gt; section,
  53. navigation, etc.:
  54. </para>
  55. <programlisting language="php"><![CDATA[
  56. <!DOCTYPE html
  57. PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  58. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  59. <html>
  60. <head>
  61. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  62. <?php echo $this->headTitle() ?>
  63. <?php echo $this->headScript() ?>
  64. <?php echo $this->headStyle() ?>
  65. </head>
  66. <body>
  67. <?php echo $this->render('header.phtml') ?>
  68. <div id="nav"><?php echo $this->placeholder('nav') ?></div>
  69. <div id="content"><?php echo $this->layout()->content ?></div>
  70. <?php echo $this->render('footer.phtml') ?>
  71. </body>
  72. </html>
  73. ]]></programlisting>
  74. </sect2>
  75. <sect2 id="zend.layout.quickstart.mvc">
  76. <title>Using Zend_Layout with the Zend Framework MVC</title>
  77. <para>
  78. <classname>Zend_Controller</classname> offers a rich set of functionality for
  79. extension via its <link linkend="zend.controller.plugins">front
  80. controller plugins</link> and <link
  81. linkend="zend.controller.actionhelpers">action controller
  82. helpers</link>. <classname>Zend_View</classname> also has <link
  83. linkend="zend.view.helpers">helpers</link>. <classname>Zend_Layout</classname>
  84. takes advantage of these various extension points when used with the
  85. <acronym>MVC</acronym> components.
  86. </para>
  87. <para>
  88. <methodname>Zend_Layout::startMvc()</methodname> creates an instance of
  89. <classname>Zend_Layout</classname> with any optional configuration you provide
  90. it. It then registers a front controller plugin that renders the
  91. layout with any application content once the dispatch loop is done,
  92. and registers an action helper to allow access to the layout object
  93. from your action controllers. Additionally, you may at any time grab
  94. the layout instance from within a view script using the
  95. <classname>Layout</classname> view helper.
  96. </para>
  97. <para>
  98. First, let's look at how to initialize <classname>Zend_Layout</classname> for use with
  99. the <acronym>MVC</acronym>:
  100. </para>
  101. <programlisting language="php"><![CDATA[
  102. // In your bootstrap:
  103. Zend_Layout::startMvc();
  104. ]]></programlisting>
  105. <para>
  106. <methodname>startMvc()</methodname> can take an optional array of options or
  107. <classname>Zend_Config</classname> object to customize the instance; these
  108. options are detailed in <link linkend="zend.layout.options">this section</link>.
  109. </para>
  110. <para>
  111. In an action controller, you may then access the layout instance as
  112. an action helper:
  113. </para>
  114. <programlisting language="php"><![CDATA[
  115. class FooController extends Zend_Controller_Action
  116. {
  117. public function barAction()
  118. {
  119. // disable layouts for this action:
  120. $this->_helper->layout->disableLayout();
  121. }
  122. public function bazAction()
  123. {
  124. // use different layout script with this action:
  125. $this->_helper->layout->setLayout('foobaz');
  126. };
  127. }
  128. ]]></programlisting>
  129. <para>
  130. In your view scripts, you can then access the layout object via the
  131. <classname>Layout</classname> view helper. This view helper is slightly
  132. different than others in that it takes no arguments, and returns an
  133. object instead of a string value. This allows you to immediately
  134. call methods on the layout object:
  135. </para>
  136. <programlisting language="php"><![CDATA[
  137. <?php $this->layout()->setLayout('foo'); // set alternate layout ?>
  138. ]]></programlisting>
  139. <para>
  140. At any time, you can fetch the <classname>Zend_Layout</classname> instance
  141. registered with the <acronym>MVC</acronym> via the
  142. <methodname>getMvcInstance()</methodname> static method:
  143. </para>
  144. <programlisting language="php"><![CDATA[
  145. // Returns null if startMvc() has not first been called
  146. $layout = Zend_Layout::getMvcInstance();
  147. ]]></programlisting>
  148. <para>
  149. Finally, <classname>Zend_Layout</classname>'s front controller plugin has one
  150. important feature in addition to rendering the layout: it retrieves
  151. all named segments from the response object and assigns them as
  152. layout variables, assigning the 'default' segment to the variable
  153. 'content'. This allows you to access your application content and
  154. render it in your view scripts.
  155. </para>
  156. <para>
  157. As an example, let's say your code first hits
  158. <methodname>FooController::indexAction()</methodname>, which renders some
  159. content to the default response segment, and then forwards to
  160. <methodname>NavController::menuAction()</methodname>, which renders content to
  161. the 'nav' response segment. Finally, you forward to
  162. <methodname>CommentController::fetchAction()</methodname> and fetch some
  163. comments, but render those to the default response segment as well
  164. (which appends content to that segment). Your view script could then
  165. render each separately:
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. <body>
  169. <!-- renders /nav/menu -->
  170. <div id="nav"><?php echo $this->layout()->nav ?></div>
  171. <!-- renders /foo/index + /comment/fetch -->
  172. <div id="content"><?php echo $this->layout()->content ?></div>
  173. </body>
  174. ]]></programlisting>
  175. <para>
  176. This feature is particularly useful when used in conjunction with
  177. the ActionStack <link linkend="zend.controller.actionhelpers.actionstack">action
  178. helper</link> and <link
  179. linkend="zend.controller.plugins.standard.actionstack">plugin</link>,
  180. which you can use to setup a stack of actions through
  181. which to loop, and thus create widgetized pages.
  182. </para>
  183. </sect2>
  184. <sect2 id="zend.layout.quickstart.standalone">
  185. <title>Using Zend_Layout as a Standalone Component</title>
  186. <para>
  187. As a standalone component, <classname>Zend_Layout</classname> does not offer nearly as
  188. many features or as much convenience as when used with the <acronym>MVC</acronym>.
  189. However, it still has two chief benefits:
  190. </para>
  191. <itemizedlist>
  192. <listitem><para>Scoping of layout variables.</para></listitem>
  193. <listitem>
  194. <para>Isolation of layout view script from other view scripts.</para>
  195. </listitem>
  196. </itemizedlist>
  197. <para>
  198. When used as a standalone component, simply instantiate the layout
  199. object, use the various accessors to set state, set variables as
  200. object properties, and render the layout:
  201. </para>
  202. <programlisting language="php"><![CDATA[
  203. $layout = new Zend_Layout();
  204. // Set a layout script path:
  205. $layout->setLayoutPath('/path/to/layouts');
  206. // set some variables:
  207. $layout->content = $content;
  208. $layout->nav = $nav;
  209. // choose a different layout script:
  210. $layout->setLayout('foo');
  211. // render final layout
  212. echo $layout->render();
  213. ]]></programlisting>
  214. </sect2>
  215. <sect2 id="zend.layout.quickstart.example">
  216. <title>Sample Layout</title>
  217. <para>
  218. Sometimes a picture is worth a thousand words. The following is a
  219. sample layout script showing how it might all come together.
  220. </para>
  221. <para>
  222. <inlinegraphic align="center" valign="middle"
  223. fileref="figures/zend.layout.quickstart.example.png" format="PNG" />
  224. </para>
  225. <para>
  226. The actual order of elements may vary, depending on the <acronym>CSS</acronym> you've
  227. setup; for instance, if you're using absolute positioning, you may
  228. be able to have the navigation displayed later in the document, but
  229. still show up at the top; the same could be said for the sidebar or
  230. header. The actual mechanics of pulling the content remain the same,
  231. however.
  232. </para>
  233. </sect2>
  234. </sect1>
  235. <!--
  236. vim:se ts=4 sw=4 et:
  237. -->