Zend_View-Helpers-Partial.xml 7.6 KB

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