Zend_Layout-Advanced.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.layout.advanced">
  4. <title>Zend_Layout Advanced Usage</title>
  5. <para>
  6. <classname>Zend_Layout</classname> has a number of use cases for the advanced
  7. developer who wishes to adapt it for different view implementations,
  8. file system layouts, and more.
  9. </para>
  10. <para>
  11. The major points of extension are:
  12. </para>
  13. <itemizedlist>
  14. <listitem>
  15. <para>
  16. <emphasis>Custom view objects.</emphasis>
  17. <classname>Zend_Layout</classname> allows you to utilize any class that
  18. implements <classname>Zend_View_Interface</classname>.
  19. </para>
  20. </listitem>
  21. <listitem>
  22. <para>
  23. <emphasis>Custom front controller plugins.</emphasis>
  24. <classname>Zend_Layout</classname> ships with a standard front controller
  25. plugin that automates rendering of layouts prior to returning
  26. the response. You can substitute your own plugin.
  27. </para>
  28. </listitem>
  29. <listitem>
  30. <para>
  31. <emphasis>Custom action helpers.</emphasis>
  32. <classname>Zend_Layout</classname> ships with a standard action helper
  33. that should be suitable for most needs as it is a dumb proxy
  34. to the layout object itself.
  35. </para>
  36. </listitem>
  37. <listitem>
  38. <para>
  39. <emphasis>Custom layout script path resolution</emphasis>.
  40. <classname>Zend_Layout</classname> allows you to use your own <link
  41. linkend="zend.filter.inflector">inflector</link> for layout
  42. script path resolution, or simply to modify the attached
  43. inflector to specify your own inflection rules.
  44. </para>
  45. </listitem>
  46. </itemizedlist>
  47. <sect2 id="zend.layout.advanced.view">
  48. <title>Custom View Objects</title>
  49. <para>
  50. <classname>Zend_Layout</classname> allows you to use any class implementing
  51. <classname>Zend_View_Interface</classname> or extending
  52. <classname>Zend_View_Abstract</classname> for rendering your layout script.
  53. Simply pass in your custom view object as a parameter to the
  54. constructor/<methodname>startMvc()</methodname>, or set it using the
  55. <methodname>setView()</methodname> accessor:
  56. </para>
  57. <programlisting language="php"><![CDATA[
  58. $view = new My_Custom_View();
  59. $layout->setView($view);
  60. ]]></programlisting>
  61. <note>
  62. <title>Not all Zend_View implementations are equal</title>
  63. <para>
  64. While <classname>Zend_Layout</classname> allows you to use any class
  65. implementing <classname>Zend_View_Interface</classname>, you may run into
  66. issues if they can not utilize the various
  67. <classname>Zend_View</classname> helpers, particularly the layout and
  68. <link linkend="zend.view.helpers.initial.placeholder">placeholder</link>
  69. helpers. This is because <classname>Zend_Layout</classname> makes
  70. variables set in the object available via itself and
  71. <link linkend="zend.view.helpers.initial.placeholder">placeholders</link>.
  72. </para>
  73. <para>
  74. If you need to use a custom <classname>Zend_View</classname>
  75. implementation that does not support these helpers, you will
  76. need to find a way to get the layout variables to the view. This
  77. can be done by either extending the <classname>Zend_Layout</classname>
  78. object and altering the <methodname>render()</methodname> method to pass
  79. variables to the view, or creating your own plugin class that
  80. passes them prior to rendering the layout.
  81. </para>
  82. <para>
  83. Alternately, if your view implementation supports any sort of plugin capability, you
  84. can access the variables via the 'Zend_Layout' placeholder, using the <link
  85. linkend="zend.view.helpers.initial.placeholder">placeholder helper</link>:
  86. </para>
  87. <programlisting language="php"><![CDATA[
  88. $placeholders = new Zend_View_Helper_Placeholder();
  89. $layoutVars = $placeholders->placeholder('Zend_Layout')->getArrayCopy();
  90. ]]></programlisting>
  91. </note>
  92. </sect2>
  93. <sect2 id="zend.layout.advanced.plugin">
  94. <title>Custom Front Controller Plugins</title>
  95. <para>
  96. When used with the <acronym>MVC</acronym> components, <classname>Zend_Layout</classname>
  97. registers a front controller plugin that renders the layout as the
  98. last action prior to exiting the dispatch loop. In most cases, the
  99. default plugin will be suitable, but should you desire to write
  100. your own, you can specify the name of the plugin class to load by
  101. passing the <property>pluginClass</property> option to the
  102. <methodname>startMvc()</methodname> method.
  103. </para>
  104. <para>
  105. Any plugin class you write for this purpose will need to extend
  106. <classname>Zend_Controller_Plugin_Abstract</classname>, and should accept a
  107. layout object instance as an argument to the constructor. Otherwise,
  108. the details of your implementation are up to you.
  109. </para>
  110. <para>
  111. The default plugin class used is
  112. <classname>Zend_Layout_Controller_Plugin_Layout</classname>.
  113. </para>
  114. </sect2>
  115. <sect2 id="zend.layout.advanced.helper">
  116. <title>Custom Action Helpers</title>
  117. <para>
  118. When used with the <acronym>MVC</acronym> components, <classname>Zend_Layout</classname>
  119. registers an action controller helper with the helper broker. The
  120. default helper,
  121. <classname>Zend_Layout_Controller_Action_Helper_Layout</classname>, acts as a
  122. dumb proxy to the layout object instance itself, and should be
  123. suitable for most use cases.
  124. </para>
  125. <para>
  126. Should you feel the need to write custom functionality, simply write
  127. an action helper class extending
  128. <classname>Zend_Controller_Action_Helper_Abstract</classname> and pass the
  129. class name as the <property>helperClass</property> option to the
  130. <methodname>startMvc()</methodname> method. Details of the implementation are
  131. up to you.
  132. </para>
  133. </sect2>
  134. <sect2 id="zend.layout.advanced.inflector">
  135. <title>Custom Layout Script Path Resolution: Using the Inflector</title>
  136. <para>
  137. <classname>Zend_Layout</classname> uses <classname>Zend_Filter_Inflector</classname> to
  138. establish a filter chain for translating a layout name to a layout
  139. script path. By default, it uses the rules 'Word_CamelCaseToDash'
  140. followed by 'StringToLower', and the suffix 'phtml' to transform the
  141. name to a path. As some examples:
  142. </para>
  143. <itemizedlist>
  144. <listitem>
  145. <para>'foo' will be transformed to 'foo.phtml'.</para>
  146. </listitem>
  147. <listitem>
  148. <para>'FooBarBaz' will be transformed to 'foo-bar-baz.phtml'.</para>
  149. </listitem>
  150. </itemizedlist>
  151. <para>
  152. You have three options for modifying inflection: modify the
  153. inflection target and/or view suffix via <classname>Zend_Layout</classname>
  154. accessors, modify the inflector rules and target of the inflector
  155. associated with the <classname>Zend_Layout</classname> instance, or create
  156. your own inflector instance and pass it to
  157. <methodname>Zend_Layout::setInflector()</methodname>.
  158. </para>
  159. <example id="zend.layout.advanced.inflector.accessors">
  160. <title>Using Zend_Layout accessors to modify the inflector</title>
  161. <para>
  162. The default <classname>Zend_Layout</classname> inflector uses static
  163. references for the target and view script suffix, and
  164. has accessors for setting these values.
  165. </para>
  166. <programlisting language="php"><![CDATA[
  167. // Set the inflector target:
  168. $layout->setInflectorTarget('layouts/:script.:suffix');
  169. // Set the layout view script suffix:
  170. $layout->setViewSuffix('php');
  171. ]]></programlisting>
  172. </example>
  173. <example id="zend.layout.advanced.inflector.directmodification">
  174. <title>Direct modification of Zend_Layout inflector</title>
  175. <para>
  176. Inflectors have a target and one or more rules. The default
  177. target used with <classname>Zend_Layout</classname> is ':script.:suffix';
  178. ':script' is passed the registered layout name, while ':suffix'
  179. is a static rule of the inflector.
  180. </para>
  181. <para>
  182. Let's say you want the layout script to end in the suffix
  183. 'html', and that you want to separate MixedCase and camelCased words with
  184. underscores instead of dashes, and not lowercase the name.
  185. Additionally, you want it to look in a 'layouts' subdirectory
  186. for the script.
  187. </para>
  188. <programlisting language="php"><![CDATA[
  189. $layout->getInflector()->setTarget('layouts/:script.:suffix')
  190. ->setStaticRule('suffix', 'html')
  191. ->setFilterRule(array('Word_CamelCaseToUnderscore'));
  192. ]]></programlisting>
  193. </example>
  194. <example id="zend.layout.advanced.inflector.custom">
  195. <title>Custom inflectors</title>
  196. <para>
  197. In most cases, modifying the existing inflector will be enough.
  198. However, you may have an inflector you wish to use in several
  199. places, with different objects of different types.
  200. <classname>Zend_Layout</classname> supports this.
  201. </para>
  202. <programlisting language="php"><![CDATA[
  203. $inflector = new Zend_Filter_Inflector('layouts/:script.:suffix');
  204. $inflector->addRules(array(
  205. ':script' => array('Word_CamelCaseToUnderscore'),
  206. 'suffix' => 'html'
  207. ));
  208. $layout->setInflector($inflector);
  209. ]]></programlisting>
  210. </example>
  211. <note>
  212. <title>Inflection can be disabled</title>
  213. <para>
  214. Inflection can be disabled and enabled using accessors on the
  215. <classname>Zend_Layout</classname> object. This can be useful if you want
  216. to specify an absolute path for a layout view script, or know
  217. that the mechanism you will be using for specifying the layout
  218. script does not need inflection. Simply use the
  219. <methodname>enableInflector()</methodname> and
  220. <methodname>disableInflector()</methodname> methods.
  221. </para>
  222. </note>
  223. </sect2>
  224. </sect1>
  225. <!--
  226. vim:se ts=4 sw=4 et:
  227. -->