Zend_View-Helpers-Partial.xml 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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><para>
  65. <emphasis>Array</emphasis>. If an array is passed, it
  66. should be associative, as its key/value pairs are
  67. assigned to the view with keys as view variables.
  68. </para></listitem>
  69. <listitem><para>
  70. <emphasis>Object implementing toArray()
  71. method</emphasis>. If an object is passed an has a
  72. <methodname>toArray()</methodname> method, the results of
  73. <methodname>toArray()</methodname> will be assigned to the view
  74. object as view variables.
  75. </para></listitem>
  76. <listitem><para>
  77. <emphasis>Standard object</emphasis>. Any other object
  78. will assign the results of
  79. <methodname>object_get_vars()</methodname> (essentially all public
  80. properties of the object) to the view object.
  81. </para></listitem>
  82. </itemizedlist>
  83. <para>
  84. If your model is an object, you may want to have it passed
  85. <emphasis>as an object</emphasis> to the partial script, instead
  86. of serializing it to an array of variables. You can do this by
  87. setting the 'objectKey' property of the appropriate helper:
  88. </para>
  89. <programlisting language="php"><![CDATA[
  90. // Tell partial to pass objects as 'model' variable
  91. $view->partial()->setObjectKey('model');
  92. // Tell partial to pass objects from partialLoop as 'model' variable
  93. // in final partial view script:
  94. $view->partialLoop()->setObjectKey('model');
  95. ]]></programlisting>
  96. <para>
  97. This technique is particularly useful when passing
  98. <classname>Zend_Db_Table_Rowset</classname>s to
  99. <methodname>partialLoop()</methodname>, as you then have full access to your
  100. row objects within the view scripts, allowing you to call
  101. methods on them (such as retrieving values from parent or
  102. dependent rows).
  103. </para>
  104. </note>
  105. <example id="zend.view.helpers.initial.partial.partialloop">
  106. <title>Using PartialLoop to Render Iterable Models</title>
  107. <para>
  108. Typically, you'll want to use partials in a loop, to render the same
  109. content fragment many times; this way you can put large blocks of
  110. repeated content or complex display logic into a single location.
  111. However this has a performance impact, as the partial helper needs
  112. to be invoked once for each iteration.
  113. </para>
  114. <para>
  115. The <classname>PartialLoop</classname> view helper helps solve this issue. It
  116. allows you to pass an iterable item (array or object implementing
  117. <emphasis>Iterator</emphasis>) as the model. It then iterates over this,
  118. passing, the items to the partial script as the model. Items in the
  119. iterator may be any model the <classname>Partial</classname> view helper
  120. allows.
  121. </para>
  122. <para>
  123. Let's assume the following partial view script:
  124. </para>
  125. <programlisting language="php"><![CDATA[
  126. <?php // partialLoop.phtml ?>
  127. <dt><?php echo $this->key ?></dt>
  128. <dd><?php echo $this->value ?></dd>
  129. ]]></programlisting>
  130. <para>
  131. And the following "model":
  132. </para>
  133. <programlisting language="php"><![CDATA[
  134. $model = array(
  135. array('key' => 'Mammal', 'value' => 'Camel'),
  136. array('key' => 'Bird', 'value' => 'Penguin'),
  137. array('key' => 'Reptile', 'value' => 'Asp'),
  138. array('key' => 'Fish', 'value' => 'Flounder'),
  139. );
  140. ]]></programlisting>
  141. <para>
  142. In your view script, you could then invoke the
  143. <classname>PartialLoop</classname> helper:
  144. </para>
  145. <programlisting language="php"><![CDATA[
  146. <dl>
  147. <?php echo $this->partialLoop('partialLoop.phtml', $model) ?>
  148. </dl>
  149. ]]></programlisting>
  150. <programlisting language="html"><![CDATA[
  151. <dl>
  152. <dt>Mammal</dt>
  153. <dd>Camel</dd>
  154. <dt>Bird</dt>
  155. <dd>Penguin</dd>
  156. <dt>Reptile</dt>
  157. <dd>Asp</dd>
  158. <dt>Fish</dt>
  159. <dd>Flounder</dd>
  160. </dl>
  161. ]]></programlisting>
  162. </example>
  163. <example id="zend.view.helpers.initial.partial.modules">
  164. <title>Rendering Partials in Other Modules</title>
  165. <para>
  166. Sometime a partial will exist in a different module. If you know the
  167. name of the module, you can pass it as the second argument to either
  168. <methodname>partial()</methodname> or <methodname>partialLoop()</methodname>, moving the
  169. <varname>$model</varname> argument to third position.
  170. </para>
  171. <para>
  172. For instance, if there's a pager partial you wish to use that's in
  173. the 'list' module, you could grab it as follows:
  174. </para>
  175. <programlisting language="php"><![CDATA[
  176. <?php echo $this->partial('pager.phtml', 'list', $pagerData) ?>
  177. ]]></programlisting>
  178. <para>
  179. In this way, you can re-use partials created specifically for other
  180. modules. That said, it's likely a better practice to put re-usable
  181. partials in shared view script paths.
  182. </para>
  183. </example>
  184. </sect3>
  185. <!--
  186. vim:se ts=4 sw=4 et:
  187. -->