2
0

Zend_View-Introduction.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 PHP as
  14. 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 PHP 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 <code>setBasePath()</code>,
  114. <code>addBasePath()</code>, 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 <code>htmlentities()</code>,
  121. <code>htmlspecialchars()</code>, and other operations. Defaults
  122. to ISO-8859-1 (latin1). May be set via
  123. <code>setEncoding()</code> 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. <code>escape()</code>. May be set via <code>setEscape()</code>
  129. or the <code>escape</code> option to the constructor.
  130. </para></listitem>
  131. <listitem><para>
  132. <code>filter</code>: indicate a filter to use after rendering
  133. a view script. May be set via <code>setFilter()</code>,
  134. <code>addFilter()</code>, or the <code>filter</code> option to
  135. the constructor.
  136. </para></listitem>
  137. <listitem><para>
  138. <code>strictVars:</code> force <classname>Zend_View</classname> to emit
  139. notices and warnings when uninitialized view variables are
  140. accessed. This may be set by calling
  141. <code>strictVars(true)</code> or passing the
  142. <code>strictVars</code> option to the constructor.
  143. </para></listitem>
  144. </itemizedlist>
  145. </sect2>
  146. <sect2 id="zend.view.introduction.shortTags">
  147. <title>Short Tags with View Scripts</title>
  148. <para>
  149. In our examples and documentation, we make use of PHP short tags:
  150. <code>&lt;?</code> and <code>&lt;?=</code>. In addition, we
  151. typically use the <ulink
  152. url="http://us.php.net/manual/en/control-structures.alternative-syntax.php">alternate
  153. syntax for control structures</ulink>. These are convenient
  154. shorthands to use when writing view scripts, as they make the
  155. constructs more terse, and keep statements on single lines.
  156. </para>
  157. <para>
  158. That said, many developers prefer to use full tags for purposes of
  159. validation or portability. For instance,
  160. <code>short_open_tag</code> is disabled in the php.ini.recommended
  161. file, and if you template XML in view scripts, short open tags will
  162. cause the templates to fail validation.
  163. </para>
  164. <para>
  165. Additionally, if you use short tags when the setting is off, then
  166. the view scripts will either cause errors or simply echo code to the
  167. user.
  168. </para>
  169. <para>
  170. For this latter case, where you wish to use short tags but they are
  171. disabled, you have two options:
  172. </para>
  173. <itemizedlist>
  174. <listitem>
  175. <para>
  176. Turn on short tags in your <code>.htaccess</code> file:
  177. </para>
  178. <programlisting language="apache"><![CDATA[
  179. php_value "short_open_tag" "on"
  180. ]]></programlisting>
  181. <para>
  182. This will only be possible if you are allowed to create and
  183. utilize <code>.htaccess</code> files. This directive can
  184. also be added to your <code>httpd.conf</code> file.
  185. </para>
  186. </listitem>
  187. <listitem>
  188. <para>
  189. Enable an optional stream wrapper to convert short tags to
  190. long tags on the fly:
  191. </para>
  192. <programlisting language="php"><![CDATA[
  193. $view->setUseStreamWrapper(true);
  194. ]]></programlisting>
  195. <para>
  196. This registers <classname>Zend_View_Stream</classname> as a stream
  197. wrapper for view scripts, and will ensure that your code
  198. continues to work as if short tags were enabled.
  199. </para>
  200. </listitem>
  201. </itemizedlist>
  202. <note>
  203. <title>View Stream Wrapper Degrades Performance</title>
  204. <para>
  205. Usage of the stream wrapper <emphasis>will</emphasis> degrade
  206. performance of your application, though actual benchmarks are
  207. unavailable to quantify the amount of degradation. We recommend
  208. that you either enable short tags, convert your scripts to use
  209. full tags, or have a good partial and/or full page content
  210. caching strategy in place.
  211. </para>
  212. </note>
  213. </sect2>
  214. <sect2 id="zend.view.introduction.accessors">
  215. <title>Utility Accessors</title>
  216. <para>
  217. Typically, you'll only ever need to call on <code>assign()</code>,
  218. <code>render()</code>, or one of the methods for setting/adding
  219. filter, helper, and script paths. However, if you wish to extend
  220. <classname>Zend_View</classname> yourself, or need access to some of its
  221. internals, a number of accessors exist:
  222. </para>
  223. <itemizedlist>
  224. <listitem>
  225. <para>
  226. <code>getVars()</code> will return all assigned variables.
  227. </para>
  228. </listitem>
  229. <listitem>
  230. <para>
  231. <code>clearVars()</code> will clear all assigned variables;
  232. useful when you wish to re-use a view object, but want to
  233. control what variables are available.
  234. </para>
  235. </listitem>
  236. <listitem>
  237. <para>
  238. <code>getScriptPath($script)</code> will retrieve the
  239. resolved path to a given view script.
  240. </para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. <code>getScriptPaths()</code> will retrieve all registered
  245. script paths.
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <code>getHelperPath($helper)</code> will retrieve the
  251. resolved path to the named helper class.
  252. </para>
  253. </listitem>
  254. <listitem>
  255. <para>
  256. <code>getHelperPaths()</code> will retrieve all registered
  257. helper paths.
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <code>getFilterPath($filter)</code> will retrieve the
  263. resolved path to the named filter class.
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <code>getFilterPaths()</code> will retrieve all registered
  269. filter paths.
  270. </para>
  271. </listitem>
  272. </itemizedlist>
  273. </sect2>
  274. </sect1>
  275. <!--
  276. vim:se ts=4 sw=4 et:
  277. -->