Zend_View-Introduction.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.view.introduction">
  4. <title>Introduction</title>
  5. <para>
  6. <classname>Zend_View</classname> is a class for working with the "view" portion of
  7. the model-view-controller pattern. That is, it exists to
  8. help keep the view script separate from the model and
  9. controller scripts. It provides a system of helpers, output
  10. filters, and variable escaping.
  11. </para>
  12. <para>
  13. <classname>Zend_View</classname> is template system agnostic; you may use
  14. <acronym>PHP</acronym> as your template language, or create instances of other
  15. template systems and manipulate them within your view
  16. script.
  17. </para>
  18. <para>
  19. Essentially, using <classname>Zend_View</classname> happens in two major steps:
  20. 1. Your controller script creates an instance of
  21. <classname>Zend_View</classname> and assigns variables to that instance.
  22. 2. The controller tells the <classname>Zend_View</classname> to render a particular
  23. view, thereby handing control over the view script, which
  24. generates the view output.
  25. </para>
  26. <sect2 id="zend.view.introduction.controller">
  27. <title>Controller Script</title>
  28. <para>
  29. As a simple example, let us say your controller has a list
  30. of book data that it wants to have rendered by a view. The
  31. controller script might look something like this:
  32. </para>
  33. <programlisting language="php"><![CDATA[
  34. // use a model to get the data for book authors and titles.
  35. $data = array(
  36. array(
  37. 'author' => 'Hernando de Soto',
  38. 'title' => 'The Mystery of Capitalism'
  39. ),
  40. array(
  41. 'author' => 'Henry Hazlitt',
  42. 'title' => 'Economics in One Lesson'
  43. ),
  44. array(
  45. 'author' => 'Milton Friedman',
  46. 'title' => 'Free to Choose'
  47. )
  48. );
  49. // now assign the book data to a Zend_View instance
  50. Zend_Loader::loadClass('Zend_View');
  51. $view = new Zend_View();
  52. $view->books = $data;
  53. // and render a view script called "booklist.php"
  54. echo $view->render('booklist.php');
  55. ]]></programlisting>
  56. </sect2>
  57. <sect2 id="zend.view.introduction.view">
  58. <title>View Script</title>
  59. <para>
  60. Now we need the associated view script, "booklist.php".
  61. This is a <acronym>PHP</acronym> script like any other, with one exception: it
  62. executes inside the scope of the <classname>Zend_View</classname> instance, which
  63. means that references to $this point to the <classname>Zend_View</classname>
  64. instance properties and methods. (Variables assigned to the
  65. instance by the controller are public properties of the
  66. <classname>Zend_View</classname> instance). Thus, a very basic view script could
  67. look like this:
  68. </para>
  69. <programlisting language="php"><![CDATA[
  70. if ($this->books): ?>
  71. <!-- A table of some books. -->
  72. <table>
  73. <tr>
  74. <th>Author</th>
  75. <th>Title</th>
  76. </tr>
  77. <?php foreach ($this->books as $key => $val): ?>
  78. <tr>
  79. <td><?php echo $this->escape($val['author']) ?></td>
  80. <td><?php echo $this->escape($val['title']) ?></td>
  81. </tr>
  82. <?php endforeach; ?>
  83. </table>
  84. <?php else: ?>
  85. <p>There are no books to display.</p>
  86. <?php endif;?>
  87. ]]></programlisting>
  88. <para>
  89. Note how we use the "escape()" method to apply output
  90. escaping to variables.
  91. </para>
  92. </sect2>
  93. <sect2 id="zend.view.introduction.options">
  94. <title>Options</title>
  95. <para>
  96. <classname>Zend_View</classname> has several options that may be set to
  97. configure the behaviour of your view scripts.
  98. </para>
  99. <itemizedlist>
  100. <listitem>
  101. <para>
  102. <code>basePath</code>: indicate a base path from which to set
  103. the script, helper, and filter path. It assumes a directory
  104. structure of:
  105. </para>
  106. <programlisting language="php"><![CDATA[
  107. base/path/
  108. helpers/
  109. filters/
  110. scripts/
  111. ]]></programlisting>
  112. <para>
  113. This may be set via <methodname>setBasePath()</methodname>,
  114. <methodname>addBasePath()</methodname>, or the <code>basePath</code>
  115. option to the constructor.
  116. </para>
  117. </listitem>
  118. <listitem><para>
  119. <code>encoding</code>: indicate the character encoding to use
  120. with <methodname>htmlentities()</methodname>,
  121. <methodname>htmlspecialchars()</methodname>, and other operations. Defaults
  122. to ISO-8859-1 (latin1). May be set via
  123. <methodname>setEncoding()</methodname> or the <code>encoding</code> option
  124. to the constructor.
  125. </para></listitem>
  126. <listitem><para>
  127. <code>escape</code>: indicate a callback to be used by
  128. <methodname>escape()</methodname>. May be set via
  129. <methodname>setEscape()</methodname> or the <code>escape</code> option to the
  130. constructor.
  131. </para></listitem>
  132. <listitem><para>
  133. <code>filter</code>: indicate a filter to use after rendering
  134. a view script. May be set via <methodname>setFilter()</methodname>,
  135. <methodname>addFilter()</methodname>, or the <code>filter</code> option to
  136. the constructor.
  137. </para></listitem>
  138. <listitem><para>
  139. <code>strictVars:</code> force <classname>Zend_View</classname> to emit
  140. notices and warnings when uninitialized view variables are
  141. accessed. This may be set by calling
  142. <methodname>strictVars(true)</methodname> or passing the
  143. <code>strictVars</code> option to the constructor.
  144. </para></listitem>
  145. </itemizedlist>
  146. </sect2>
  147. <sect2 id="zend.view.introduction.shortTags">
  148. <title>Short Tags with View Scripts</title>
  149. <para>
  150. In our examples, we make use of <acronym>PHP</acronym> long tags: <code>&lt;?php</code>.
  151. We also favor the use of <ulink
  152. url="http://us.php.net/manual/en/control-structures.alternative-syntax.php">alternate
  153. syntax for control structures</ulink>. These are convenient shorthands to use when
  154. writing view scripts, as they make the constructs more terse, keep statements on single
  155. lines, and eliminate the need to hunt for brackets within HTML.
  156. </para>
  157. <para>
  158. In previous versions, we often recommended using short tags (<code>&lt;?</code> and
  159. <code>&lt;?=</code>), as they make the view scripts slightly less verbose. However, the
  160. default for the <filename>php.ini</filename> <constant>short_open_tag</constant> setting
  161. is typically off in production or on shared hosts -- making their use not terribly
  162. portable. If you use template <acronym>XML</acronym> in view scripts, short
  163. open tags will cause the templates to fail validation. Finally, if you use short tags
  164. when <constant>short_open_tag</constant> is off, the view scripts will either cause
  165. errors or simply echo PHP code back to the viewer.
  166. </para>
  167. <para>
  168. If, despite these warnings, you wish to use short tags but they are disabled, you have
  169. two options:
  170. </para>
  171. <itemizedlist>
  172. <listitem>
  173. <para>
  174. Turn on short tags in your <code>.htaccess</code> file:
  175. </para>
  176. <programlisting language="apache"><![CDATA[
  177. php_value "short_open_tag" "on"
  178. ]]></programlisting>
  179. <para>
  180. This will only be possible if you are allowed to create and
  181. utilize <code>.htaccess</code> files. This directive can
  182. also be added to your <code>httpd.conf</code> file.
  183. </para>
  184. </listitem>
  185. <listitem>
  186. <para>
  187. Enable an optional stream wrapper to convert short tags to
  188. long tags on the fly:
  189. </para>
  190. <programlisting language="php"><![CDATA[
  191. $view->setUseStreamWrapper(true);
  192. ]]></programlisting>
  193. <para>
  194. This registers <classname>Zend_View_Stream</classname> as a stream
  195. wrapper for view scripts, and will ensure that your code
  196. continues to work as if short tags were enabled.
  197. </para>
  198. </listitem>
  199. </itemizedlist>
  200. <warning>
  201. <title>View Stream Wrapper Degrades Performance</title>
  202. <para>
  203. Usage of the stream wrapper <emphasis>will</emphasis> degrade
  204. performance of your application, though actual benchmarks are
  205. unavailable to quantify the amount of degradation. We recommend
  206. that you either enable short tags, convert your scripts to use
  207. full tags, or have a good partial and/or full page content
  208. caching strategy in place.
  209. </para>
  210. </warning>
  211. </sect2>
  212. <sect2 id="zend.view.introduction.accessors">
  213. <title>Utility Accessors</title>
  214. <para>
  215. Typically, you'll only ever need to call on <methodname>assign()</methodname>,
  216. <methodname>render()</methodname>, or one of the methods for setting/adding
  217. filter, helper, and script paths. However, if you wish to extend
  218. <classname>Zend_View</classname> yourself, or need access to some of its
  219. internals, a number of accessors exist:
  220. </para>
  221. <itemizedlist>
  222. <listitem>
  223. <para>
  224. <methodname>getVars()</methodname> will return all assigned variables.
  225. </para>
  226. </listitem>
  227. <listitem>
  228. <para>
  229. <methodname>clearVars()</methodname> will clear all assigned variables;
  230. useful when you wish to re-use a view object, but want to
  231. control what variables are available.
  232. </para>
  233. </listitem>
  234. <listitem>
  235. <para>
  236. <methodname>getScriptPath($script)</methodname> will retrieve the
  237. resolved path to a given view script.
  238. </para>
  239. </listitem>
  240. <listitem>
  241. <para>
  242. <methodname>getScriptPaths()</methodname> will retrieve all registered
  243. script paths.
  244. </para>
  245. </listitem>
  246. <listitem>
  247. <para>
  248. <methodname>getHelperPath($helper)</methodname> will retrieve the
  249. resolved path to the named helper class.
  250. </para>
  251. </listitem>
  252. <listitem>
  253. <para>
  254. <methodname>getHelperPaths()</methodname> will retrieve all registered
  255. helper paths.
  256. </para>
  257. </listitem>
  258. <listitem>
  259. <para>
  260. <methodname>getFilterPath($filter)</methodname> will retrieve the
  261. resolved path to the named filter class.
  262. </para>
  263. </listitem>
  264. <listitem>
  265. <para>
  266. <methodname>getFilterPaths()</methodname> will retrieve all registered
  267. filter paths.
  268. </para>
  269. </listitem>
  270. </itemizedlist>
  271. </sect2>
  272. </sect1>
  273. <!--
  274. vim:se ts=4 sw=4 et:
  275. -->