Zend_View-Introduction.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. <property>basePath</property>: 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 <property>basePath</property>
  115. option to the constructor.
  116. </para>
  117. </listitem>
  118. <listitem><para>
  119. <property>encoding</property>: 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 <property>encoding</property> option
  124. to the constructor.
  125. </para></listitem>
  126. <listitem><para>
  127. <property>escape</property>: indicate a callback to be used by
  128. <methodname>escape()</methodname>. May be set via
  129. <methodname>setEscape()</methodname> or the <property>escape</property> option to
  130. the constructor.
  131. </para></listitem>
  132. <listitem><para>
  133. <property>filter</property>: indicate a filter to use after rendering
  134. a view script. May be set via <methodname>setFilter()</methodname>,
  135. <methodname>addFilter()</methodname>, or the <property>filter</property> option to
  136. the constructor.
  137. </para></listitem>
  138. <listitem><para>
  139. <property>strictVars</property>: 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. <property>strictVars</property> 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:
  151. <emphasis>&lt;?php</emphasis>. 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 (<emphasis>&lt;?</emphasis>
  159. and <emphasis>&lt;?=</emphasis>), as they make the view scripts slightly less verbose.
  160. However, the default for the <filename>php.ini</filename>
  161. <constant>short_open_tag</constant> setting is typically off in production or on shared
  162. hosts -- making their use not terribly portable. If you use template
  163. <acronym>XML</acronym> in view scripts, short open tags will cause the templates to fail
  164. validation. Finally, if you use short tags when <constant>short_open_tag</constant> is
  165. off, the view scripts will either cause errors or simply echo PHP code back to the
  166. viewer.
  167. </para>
  168. <para>
  169. If, despite these warnings, you wish to use short tags but they are disabled, you have
  170. two options:
  171. </para>
  172. <itemizedlist>
  173. <listitem>
  174. <para>
  175. Turn on short tags in your <filename>.htaccess</filename> file:
  176. </para>
  177. <programlisting language="apache"><![CDATA[
  178. php_value "short_open_tag" "on"
  179. ]]></programlisting>
  180. <para>
  181. This will only be possible if you are allowed to create and
  182. utilize <filename>.htaccess</filename> files. This directive can
  183. also be added to your <filename>httpd.conf</filename> file.
  184. </para>
  185. </listitem>
  186. <listitem>
  187. <para>
  188. Enable an optional stream wrapper to convert short tags to
  189. long tags on the fly:
  190. </para>
  191. <programlisting language="php"><![CDATA[
  192. $view->setUseStreamWrapper(true);
  193. ]]></programlisting>
  194. <para>
  195. This registers <classname>Zend_View_Stream</classname> as a stream
  196. wrapper for view scripts, and will ensure that your code
  197. continues to work as if short tags were enabled.
  198. </para>
  199. </listitem>
  200. </itemizedlist>
  201. <warning>
  202. <title>View Stream Wrapper Degrades Performance</title>
  203. <para>
  204. Usage of the stream wrapper <emphasis>will</emphasis> degrade
  205. performance of your application, though actual benchmarks are
  206. unavailable to quantify the amount of degradation. We recommend
  207. that you either enable short tags, convert your scripts to use
  208. full tags, or have a good partial and/or full page content
  209. caching strategy in place.
  210. </para>
  211. </warning>
  212. </sect2>
  213. <sect2 id="zend.view.introduction.accessors">
  214. <title>Utility Accessors</title>
  215. <para>
  216. Typically, you'll only ever need to call on <methodname>assign()</methodname>,
  217. <methodname>render()</methodname>, or one of the methods for setting/adding
  218. filter, helper, and script paths. However, if you wish to extend
  219. <classname>Zend_View</classname> yourself, or need access to some of its
  220. internals, a number of accessors exist:
  221. </para>
  222. <itemizedlist>
  223. <listitem>
  224. <para>
  225. <methodname>getVars()</methodname> will return all assigned variables.
  226. </para>
  227. </listitem>
  228. <listitem>
  229. <para>
  230. <methodname>clearVars()</methodname> will clear all assigned variables;
  231. useful when you wish to re-use a view object, but want to
  232. control what variables are available.
  233. </para>
  234. </listitem>
  235. <listitem>
  236. <para>
  237. <methodname>getScriptPath($script)</methodname> will retrieve the
  238. resolved path to a given view script.
  239. </para>
  240. </listitem>
  241. <listitem>
  242. <para>
  243. <methodname>getScriptPaths()</methodname> will retrieve all registered
  244. script paths.
  245. </para>
  246. </listitem>
  247. <listitem>
  248. <para>
  249. <methodname>getHelperPath($helper)</methodname> will retrieve the
  250. resolved path to the named helper class.
  251. </para>
  252. </listitem>
  253. <listitem>
  254. <para>
  255. <methodname>getHelperPaths()</methodname> will retrieve all registered
  256. helper paths.
  257. </para>
  258. </listitem>
  259. <listitem>
  260. <para>
  261. <methodname>getFilterPath($filter)</methodname> will retrieve the
  262. resolved path to the named filter class.
  263. </para>
  264. </listitem>
  265. <listitem>
  266. <para>
  267. <methodname>getFilterPaths()</methodname> will retrieve all registered
  268. filter paths.
  269. </para>
  270. </listitem>
  271. </itemizedlist>
  272. </sect2>
  273. </sect1>
  274. <!--
  275. vim:se ts=4 sw=4 et:
  276. -->