2
0

Zend_View-Introduction.xml 12 KB

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