Zend_View-Helpers-Partial.xml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect3 id="zend.view.helpers.initial.partial">
  4. <title>Partial Helper</title>
  5. <para>
  6. The <classname>Partial</classname> view helper is used to render a specified
  7. template within its own variable scope. The primary use is for reusable
  8. template fragments with which you do not need to worry about variable
  9. name clashes. Additionally, they allow you to specify partial view
  10. scripts from specific modules.
  11. </para>
  12. <para>
  13. A sibling to the <classname>Partial</classname>, the <classname>PartialLoop</classname> view
  14. helper allows you to pass iterable data, and render a partial for each
  15. item.
  16. </para>
  17. <note>
  18. <title>PartialLoop Counter</title>
  19. <para>
  20. The <classname>PartialLoop</classname> view helper assigns a variable to the view named
  21. <emphasis>partialCounter</emphasis> which passes the current position of the array to
  22. the view script. This provides an easy way to have alternating colors on table rows for
  23. example.
  24. </para>
  25. </note>
  26. <example id="zend.view.helpers.initial.partial.usage">
  27. <title>Basic Usage of Partials</title>
  28. <para>
  29. Basic usage of partials is to render a template fragment in its own
  30. view scope. Consider the following partial script:
  31. </para>
  32. <programlisting language="php"><![CDATA[
  33. <?php // partial.phtml ?>
  34. <ul>
  35. <li>From: <?php echo $this->escape($this->from) ?></li>
  36. <li>Subject: <?php echo $this->escape($this->subject) ?></li>
  37. </ul>
  38. ]]></programlisting>
  39. <para>
  40. You would then call it from your view script using the following:
  41. </para>
  42. <programlisting language="php"><![CDATA[
  43. <?php echo $this->partial('partial.phtml', array(
  44. 'from' => 'Team Framework',
  45. 'subject' => 'view partials')); ?>
  46. ]]></programlisting>
  47. <para>
  48. Which would then render:
  49. </para>
  50. <programlisting language="html"><![CDATA[
  51. <ul>
  52. <li>From: Team Framework</li>
  53. <li>Subject: view partials</li>
  54. </ul>
  55. ]]></programlisting>
  56. </example>
  57. <note>
  58. <title>What is a model?</title>
  59. <para>
  60. A model used with the <classname>Partial</classname> view helper can be
  61. one of the following:
  62. </para>
  63. <itemizedlist>
  64. <listitem>
  65. <para>
  66. <emphasis>Array</emphasis>. If an array is passed, it
  67. should be associative, as its key/value pairs are
  68. assigned to the view with keys as view variables.
  69. </para>
  70. </listitem>
  71. <listitem>
  72. <para>
  73. <emphasis>Object implementing toArray() method</emphasis>. If an object is
  74. passed an has a <methodname>toArray()</methodname> method, the results of
  75. <methodname>toArray()</methodname> will be assigned to the view
  76. object as view variables.
  77. </para>
  78. </listitem>
  79. <listitem>
  80. <para>
  81. <emphasis>Standard object</emphasis>. Any other object
  82. will assign the results of
  83. <methodname>object_get_vars()</methodname> (essentially all public
  84. properties of the object) to the view object.
  85. </para>
  86. </listitem>
  87. </itemizedlist>
  88. <para>
  89. If your model is an object, you may want to have it passed
  90. <emphasis>as an object</emphasis> to the partial script, instead
  91. of serializing it to an array of variables. You can do this by
  92. setting the 'objectKey' property of the appropriate helper:
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. // Tell partial to pass objects as 'model' variable
  96. $view->partial()->setObjectKey('model');
  97. // Tell partial to pass objects from partialLoop as 'model' variable
  98. // in final partial view script:
  99. $view->partialLoop()->setObjectKey('model');
  100. ]]></programlisting>
  101. <para>
  102. This technique is particularly useful when passing
  103. <classname>Zend_Db_Table_Rowset</classname>s to
  104. <methodname>partialLoop()</methodname>, as you then have full access to your
  105. row objects within the view scripts, allowing you to call
  106. methods on them (such as retrieving values from parent or
  107. dependent rows).
  108. </para>
  109. </note>
  110. <example id="zend.view.helpers.initial.partial.partialloop">
  111. <title>Using PartialLoop to Render Iterable Models</title>
  112. <para>
  113. Typically, you'll want to use partials in a loop, to render the same
  114. content fragment many times; this way you can put large blocks of
  115. repeated content or complex display logic into a single location.
  116. However this has a performance impact, as the partial helper needs
  117. to be invoked once for each iteration.
  118. </para>
  119. <para>
  120. The <classname>PartialLoop</classname> view helper helps solve this issue. It
  121. allows you to pass an iterable item (array or object implementing
  122. <emphasis>Iterator</emphasis>) as the model. It then iterates over this,
  123. passing, the items to the partial script as the model. Items in the
  124. iterator may be any model the <classname>Partial</classname> view helper
  125. allows.
  126. </para>
  127. <para>
  128. Let's assume the following partial view script:
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. <?php // partialLoop.phtml ?>
  132. <dt><?php echo $this->key ?></dt>
  133. <dd><?php echo $this->value ?></dd>
  134. ]]></programlisting>
  135. <para>
  136. And the following "model":
  137. </para>
  138. <programlisting language="php"><![CDATA[
  139. $model = array(
  140. array('key' => 'Mammal', 'value' => 'Camel'),
  141. array('key' => 'Bird', 'value' => 'Penguin'),
  142. array('key' => 'Reptile', 'value' => 'Asp'),
  143. array('key' => 'Fish', 'value' => 'Flounder'),
  144. );
  145. ]]></programlisting>
  146. <para>
  147. In your view script, you could then invoke the
  148. <classname>PartialLoop</classname> helper:
  149. </para>
  150. <programlisting language="php"><![CDATA[
  151. <dl>
  152. <?php echo $this->partialLoop('partialLoop.phtml', $model) ?>
  153. </dl>
  154. ]]></programlisting>
  155. <programlisting language="html"><![CDATA[
  156. <dl>
  157. <dt>Mammal</dt>
  158. <dd>Camel</dd>
  159. <dt>Bird</dt>
  160. <dd>Penguin</dd>
  161. <dt>Reptile</dt>
  162. <dd>Asp</dd>
  163. <dt>Fish</dt>
  164. <dd>Flounder</dd>
  165. </dl>
  166. ]]></programlisting>
  167. </example>
  168. <example id="zend.view.helpers.initial.partial.modules">
  169. <title>Rendering Partials in Other Modules</title>
  170. <para>
  171. Sometime a partial will exist in a different module. If you know the
  172. name of the module, you can pass it as the second argument to either
  173. <methodname>partial()</methodname> or <methodname>partialLoop()</methodname>, moving the
  174. <varname>$model</varname> argument to third position.
  175. </para>
  176. <para>
  177. For instance, if there's a pager partial you wish to use that's in
  178. the 'list' module, you could grab it as follows:
  179. </para>
  180. <programlisting language="php"><![CDATA[
  181. <?php echo $this->partial('pager.phtml', 'list', $pagerData) ?>
  182. ]]></programlisting>
  183. <para>
  184. In this way, you can re-use partials created specifically for other
  185. modules. That said, it's likely a better practice to put re-usable
  186. partials in shared view script paths.
  187. </para>
  188. </example>
  189. </sect3>
  190. <!--
  191. vim:se ts=4 sw=4 et:
  192. -->