layout-usage.xml 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.layout.usage">
  4. <title>Using Zend_Layout</title>
  5. <para>
  6. Basic usage of <classname>Zend_Layout</classname> is fairly trivial. Assuming you're using
  7. <classname>Zend_Application</classname> already, you can simply provide some configuration
  8. options and create a layout view script.
  9. </para>
  10. <sect2 id="learning.layout.usage.configuration">
  11. <title>Layout Configuration</title>
  12. <para>
  13. The recommended location of layouts is in a "<filename>layouts/scripts/</filename>"
  14. subdirectory of your application:
  15. </para>
  16. <programlisting language="text"><![CDATA[
  17. application
  18. |-- Bootstrap.php
  19. |-- configs
  20. | `-- application.ini
  21. |-- controllers
  22. |-- layouts
  23. | `-- scripts
  24. | |-- layout.phtml
  25. ]]></programlisting>
  26. <para>
  27. To initialize <classname>Zend_Layout</classname>, add the following to your
  28. configuration file ("<filename>application/configs/application.ini</filename>"):
  29. </para>
  30. <programlisting language="dosini"><![CDATA[
  31. resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
  32. resources.layout.layout = "layout"
  33. ]]></programlisting>
  34. <para>
  35. The first line indicates where to look for layout scripts; the second line gives the
  36. name of the layout to use, minus the view script extension (which is assumed to be
  37. "<filename>.phtml</filename>" by default).
  38. </para>
  39. </sect2>
  40. <sect2 id="learning.layout.usage.layout-script">
  41. <title>Create a Layout Script</title>
  42. <para>
  43. Now that you have your configuration in place, you need to create your layout script.
  44. First, make sure that you've created the
  45. "<filename>application/layouts/scripts</filename>" directory; then,
  46. open an editor, and create the markup for your layout. Layout scripts are simply view
  47. scripts, with some slight differences.
  48. </para>
  49. <programlisting language="php"><![CDATA[
  50. <html>
  51. <head>
  52. <title>My Site</title>
  53. </head>
  54. <body>
  55. <?php echo $this->layout()->content ?>
  56. </body>
  57. </html>
  58. ]]></programlisting>
  59. <para>
  60. In the example above, you'll note the call to a <methodname>layout()</methodname> view
  61. helper. When you register the <classname>Zend_Layout</classname> resource, you also gain
  62. access to both an action and view helper that allow you access to the
  63. <classname>Zend_Layout</classname> instance; you can then call operations on the layout
  64. object. In this case, we're retrieving a named variable, <varname>$content</varname>,
  65. and echoing it. By default, the <varname>$content</varname> variable is populated for
  66. you from the application view script rendered. Otherwise, anything you'd normally do
  67. in a view script is perfectly valid -- call any helpers or view methods you desire.
  68. </para>
  69. <para>
  70. At this point, you have a working layout script, and your application is informed of its
  71. location and knows to render it.
  72. </para>
  73. </sect2>
  74. <sect2 id="learning.layout.usage.access">
  75. <title>Accessing the Layout Object</title>
  76. <para>
  77. On occasion, you may need direct access to the layout object. There are three ways you
  78. can do this:
  79. </para>
  80. <itemizedlist>
  81. <listitem>
  82. <para>
  83. <emphasis>Within view scripts:</emphasis> use the
  84. <methodname>layout()</methodname> view helper, which returns the
  85. <classname>Zend_Layout</classname> instance registered with the front controller
  86. plugin.
  87. </para>
  88. <programlisting language="php"><![CDATA[
  89. <?php $layout = $this->layout(); ?>
  90. ]]></programlisting>
  91. <para>
  92. Since it returns the layout instance, you can also simply call methods on it,
  93. rather than assigning it to a variable.
  94. </para>
  95. </listitem>
  96. <listitem>
  97. <para>
  98. <emphasis>Within action controllers:</emphasis> use the
  99. <methodname>layout()</methodname> action helper, which acts just like the view
  100. helper.
  101. </para>
  102. <programlisting language="php"><![CDATA[
  103. // Calling helper as a method of the helper broker:
  104. $layout = $this->_helper->layout();
  105. // Or, more verbosely:
  106. $helper = $this->_helper->getHelper('Layout');
  107. $layout = $helper->getLayoutInstance();
  108. ]]></programlisting>
  109. <para>
  110. As with the view helper, since the action helper returns the layout instance,
  111. you can also simply call methods on it, rather than assigning it to a variable.
  112. </para>
  113. </listitem>
  114. <listitem>
  115. <para>
  116. <emphasis>Elsewhere: </emphasis> use the static method
  117. <methodname>getMvcInstance()</methodname>. This will return the layout instance
  118. registered by the bootstrap resource.
  119. </para>
  120. <programlisting language="php"><![CDATA[
  121. $layout = Zend_Layout::getMvcInstance();
  122. ]]></programlisting>
  123. </listitem>
  124. <listitem>
  125. <para>
  126. <emphasis>Via the bootstrap: </emphasis> retrieve the layout resource, which
  127. will be the <classname>Zend_Layout</classname> instance.
  128. </para>
  129. <programlisting language="php"><![CDATA[
  130. $layout = $bootstrap->getResource('Layout');
  131. ]]></programlisting>
  132. <para>
  133. Anywhere you have access to the bootstrap object, this method is preferred over
  134. using the static <methodname>getMvcInstance()</methodname> method.
  135. </para>
  136. </listitem>
  137. </itemizedlist>
  138. </sect2>
  139. <sect2 id="learning.layout.usage.other-operations">
  140. <title>Other Operations</title>
  141. <para>
  142. In most cases, the above configuration and layout script (with modifications) will get
  143. you what you need. However, some other functionality exists you will likely use sooner
  144. or later. In all of the following examples, you may use one of the <link
  145. linkend="learning.layout.usage.access">methods listed above</link> for retrieving
  146. the layout object.
  147. </para>
  148. <itemizedlist>
  149. <listitem>
  150. <para>
  151. <emphasis>Setting layout variables</emphasis>.
  152. <classname>Zend_Layout</classname> keeps its own registry of layout-specific
  153. view variables that you can access; the <varname>$content</varname> key noted in
  154. the initial layout script sample is one such example. You can assign and
  155. retrieve these using normal property access, or via the
  156. <methodname>assign()</methodname> method.
  157. </para>
  158. <programlisting language="php"><![CDATA[
  159. // Setting content:
  160. $layout->somekey = "foo"
  161. // Echoing that same content:
  162. echo $layout->somekey; // 'foo'
  163. // Using the assign() method:
  164. $layout->assign('someotherkey', 'bar');
  165. // Access to assign()'d variables remains the same:
  166. echo $layout->someotherkey; // 'bar'
  167. ]]></programlisting>
  168. </listitem>
  169. <listitem>
  170. <para>
  171. <methodname>disableLayout()</methodname>. Occasionally, you may want to disable
  172. layouts; for example, when answering an Ajax request, or providing a RESTful
  173. representation of a resource. In these cases, you can call the
  174. <methodname>disableLayout()</methodname> method on your layout object.
  175. </para>
  176. <programlisting language="php"><![CDATA[
  177. $layout->disableLayout();
  178. ]]></programlisting>
  179. <para>
  180. The opposite of this method is, of course,
  181. <methodname>enableLayout()</methodname>, which can be called at any time to
  182. re-enable layouts for the requested action.
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. <emphasis>Selecting an alternate layout</emphasis>: If you have multiple
  188. layouts for your site or application, you can select the layout to use at any
  189. time by simply calling the <methodname>setLayout()</methodname> method. Call it
  190. by specifying the name of the layout script without the file suffix.
  191. </para>
  192. <programlisting language="php"><![CDATA[
  193. // Use the layout script "alternate.phtml":
  194. $layout->setLayout('alternate');
  195. ]]></programlisting>
  196. <para>
  197. The layout script should reside in the <varname>$layoutPath</varname> directory
  198. specified in your configuration. <classname>Zend_Layout</classname> will then
  199. use this new layout when rendering.
  200. </para>
  201. </listitem>
  202. </itemizedlist>
  203. </sect2>
  204. </sect1>