Zend_View-Introduction.xml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <sect1 id="zend.view.introduction">
  2. <title>Pendahuluan</title>
  3. <para>
  4. Zend_View adalah class yang dapat Anda gunakan untuk bekerja dengan
  5. bagian "view" dari pola model-view-controller. Yaitu untuk menjaga
  6. view script terpisah dari model dan controller script. Zend_View
  7. menyediakan sebuah sistem pembantu (helpers), output filters dan
  8. variable escaping.
  9. </para>
  10. <para>
  11. Zend_View is template system agnostic; you may use PHP as
  12. your template language, or create instances of other
  13. template systems and manipulate them within your view
  14. script.
  15. </para>
  16. <para>
  17. Essentially, using Zend_View happens in two major steps:
  18. 1. Your controller script creates an instance of
  19. Zend_View and assigns variables to that instance.
  20. 2. The controller tells the Zend_View to render a particular
  21. view, thereby handing control over the view script, which
  22. generates the view output.
  23. </para>
  24. <sect2 id="zend.view.introduction.controller">
  25. <title>Controller Script</title>
  26. <para>
  27. As a simple example, let us say your controller has a list
  28. of book data that it wants to have rendered by a view. The
  29. controller script might look something like this:
  30. </para>
  31. <programlisting role="php"><![CDATA[<?php
  32. // use a model to get the data for book authors and titles.
  33. $data = array(
  34. array(
  35. 'author' => 'Hernando de Soto',
  36. 'title' => 'The Mystery of Capitalism'
  37. ),
  38. array(
  39. 'author' => 'Henry Hazlitt',
  40. 'title' => 'Economics in One Lesson'
  41. ),
  42. array(
  43. 'author' => 'Milton Friedman',
  44. 'title' => 'Free to Choose'
  45. )
  46. );
  47. // now assign the book data to a Zend_View instance
  48. Zend_Loader::loadClass('Zend_View');
  49. $view = new Zend_View();
  50. $view->books = $data;
  51. // and render a view script called "booklist.php"
  52. echo $view->render('booklist.php');]]>
  53. </programlisting>
  54. </sect2>
  55. <sect2 id="zend.view.introduction.view">
  56. <title>View Script</title>
  57. <para>
  58. Now we need the associated view script, "booklist.php".
  59. This is a PHP script like any other, with one exception: it
  60. executes inside the scope of the Zend_View instance, which
  61. means that references to $this point to the Zend_View
  62. instance properties and methods. (Variables assigned to the
  63. instance by the controller are public properties of the
  64. Zend_View instance.) Thus, a very basic view script could
  65. look like this:
  66. </para>
  67. <programlisting role="php"><![CDATA[<?php if ($this->books): ?>
  68. <!-- A table of some books. -->
  69. <table>
  70. <tr>
  71. <th>Author</th>
  72. <th>Title</th>
  73. </tr>
  74. <?php foreach ($this->books as $key => $val): ?>
  75. <tr>
  76. <td><?php echo $this->escape($val['author']) ?></td>
  77. <td><?php echo $this->escape($val['title']) ?></td>
  78. </tr>
  79. <?php endforeach; ?>
  80. </table>
  81. <?php else: ?>
  82. <p>There are no books to display.</p>
  83. <?php endif;]]>
  84. </programlisting>
  85. <para>
  86. Note how we use the "escape()" method to apply output
  87. escaping to variables.
  88. </para>
  89. </sect2>
  90. <sect2 id="zend.view.introduction.options">
  91. <title>Options</title>
  92. <para>
  93. <code>Zend_View</code> has several options that may be set to
  94. configure the behaviour of your view scripts.
  95. </para>
  96. <itemizedlist>
  97. <listitem>
  98. <para>
  99. <code>basePath:</code> indicate a base path from which to set
  100. the script, helper, and filter path. It assumes a directory
  101. structure of:
  102. </para>
  103. <programlisting role="php"><![CDATA[
  104. base/path/
  105. helpers/
  106. filters/
  107. scripts/]]>
  108. </programlisting>
  109. <para>
  110. This may be set via <code>setBasePath()</code>,
  111. <code>addBasePath()</code>, or the <code>basePath</code>
  112. option to the constructor.
  113. </para>
  114. </listitem>
  115. <listitem><para>
  116. <code>encoding:</code> indicate the character encoding to use
  117. with <code>htmlentities()</code>,
  118. <code>htmlspecialchars()</code>, and other operations. Defaults
  119. to ISO-8859-1 (latin1). May be set via
  120. <code>setEncoding()</code> or the <code>encoding</code> option
  121. to the constructor.
  122. </para></listitem>
  123. <listitem><para>
  124. <code>escape:</code> indicate a callback to be used by
  125. <code>escape()</code>. May be set via <code>setEscape()</code>
  126. or the <code>escape</code> option to the constructor.
  127. </para></listitem>
  128. <listitem><para>
  129. <code>filter:</code> indicate a filter to use after rendering
  130. a view script. May be set via <code>setFilter()</code>,
  131. <code>addFilter()</code>, or the <code>filter</code> option to
  132. the constructor.
  133. </para></listitem>
  134. <listitem><para>
  135. <code>strictVars:</code> force <code>Zend_View</code> to emit
  136. notices and warnings when uninitialized view variables are
  137. accessed. This may be set by calling
  138. <code>strictVars(true)</code> or passing the
  139. <code>strictVars</code> option to the constructor.
  140. </para></listitem>
  141. </itemizedlist>
  142. </sect2>
  143. <sect2 id="zend.view.introduction.accessors">
  144. <title>Utility Accessors</title>
  145. <para>
  146. Typically, you'll only ever need to call on <code>assign()</code>,
  147. <code>render()</code>, or one of the methods for setting/adding
  148. filter, helper, and script paths. However, if you wish to extend
  149. <code>Zend_View</code> yourself, or need access to some of its
  150. internals, a number of accessors exist:
  151. </para>
  152. <itemizedlist>
  153. <listitem>
  154. <para>
  155. <code>getVars()</code> will return all assigned variables.
  156. </para>
  157. </listitem>
  158. <listitem>
  159. <para>
  160. <code>clearVars()</code> will clear all assigned variables;
  161. useful when you wish to re-use a view object, but want to
  162. control what variables are available..
  163. </para>
  164. </listitem>
  165. <listitem>
  166. <para>
  167. <code>getScriptPath($script)</code> will retrieve the
  168. resolved path to a given view script.
  169. </para>
  170. </listitem>
  171. <listitem>
  172. <para>
  173. <code>getScriptPaths()</code> will retrieve all registered
  174. script paths.
  175. </para>
  176. </listitem>
  177. <listitem>
  178. <para>
  179. <code>getHelperPath($helper)</code> will retrieve the
  180. resolved path to the named helper class.
  181. </para>
  182. </listitem>
  183. <listitem>
  184. <para>
  185. <code>getHelperPaths()</code> will retrieve all registered
  186. helper paths.
  187. </para>
  188. </listitem>
  189. <listitem>
  190. <para>
  191. <code>getFilterPath($filter)</code> will retrieve the
  192. resolved path to the named filter class.
  193. </para>
  194. </listitem>
  195. <listitem>
  196. <para>
  197. <code>getFilterPaths()</code> will retrieve all registered
  198. filter paths.
  199. </para>
  200. </listitem>
  201. </itemizedlist>
  202. </sect2>
  203. </sect1>
  204. <!--
  205. vim:se ts=4 sw=4 et:
  206. -->