Zend_View-Introduction.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 22917 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="zend.view.introduction">
  5. <title>Introdução</title>
  6. <para>
  7. <classname>Zend_View</classname> é uma classe para trabalhar com a parte de visualização do
  8. padrão de projeto MVC. Basicamente ela existe para separar o script de visualização dos
  9. controladores e modelos. Ela fornece um sistema de assistentes, filtros de saída e escape
  10. de variáveis.
  11. </para>
  12. <para>
  13. <classname>Zend_View</classname> é um sistema de template agnóstico; você pode usar o
  14. <acronym>PHP</acronym> como sua linguagem de template, ou criar instâncias de outros
  15. sistemas de template e manipulá-las dentro do seu script de visualização.
  16. </para>
  17. <para>
  18. Essencialmente, o funcionamento do <classname>Zend_View</classname> acontece em duas etapas
  19. principais:
  20. 1. Seu script controlador cria uma instância de <classname>Zend_View</classname>,
  21. atribuindo-lhe variáveis.
  22. 2. O controlador instrui o <classname>Zend_View</classname> a renderizar uma determinada
  23. visualização, passando o controle ao script de visualização, responsável pela geração da
  24. saída a ser visualizada.
  25. </para>
  26. <sect2 id="zend.view.introduction.controller">
  27. <title>Script Controlador</title>
  28. <para>
  29. Neste exemplo simples, suponhamos que seu controlador tenha uma listagem contendo dados
  30. sobre livros, que queremos renderizar para uma visualização. O controlador poderia ser
  31. algo como isto:
  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>Script Visualizador</title>
  59. <para>
  60. Agora necessitaremos do script de visualização associado, "booklist.php". Trata-se de um
  61. script <acronym>PHP</acronym> como qualquer outro, com uma exceção: ele executa dentro
  62. do escopo da instância de <classname>Zend_View</classname>, o que implica que as
  63. referências a $this apontam para as propriedades e métodos da instância
  64. <classname>Zend_View</classname>. (Variáveis atribuídas à instância pelo controlador são
  65. propriedades públicas da instância de <classname>Zend_View</classname>). Deste modo, um
  66. script de visualização muito básico poderia se parecer com isto:
  67. </para>
  68. <programlisting language="php"><![CDATA[
  69. if ($this->books): ?>
  70. <!-- Uma tabela contendo alguns livros. -->
  71. <table>
  72. <tr>
  73. <th>Autor</th>
  74. <th>Título</th>
  75. </tr>
  76. <?php foreach ($this->books as $key => $val): ?>
  77. <tr>
  78. <td><?php echo $this->escape($val['author']) ?></td>
  79. <td><?php echo $this->escape($val['title']) ?></td>
  80. </tr>
  81. <?php endforeach; ?>
  82. </table>
  83. <?php else: ?>
  84. <p>Não existem livros a serem exibidos.</p>
  85. <?php endif;?>
  86. ]]></programlisting>
  87. <para>
  88. Observe a forma como empregamos o método "escape()" para escapar o conteúdo das
  89. variáveis para a saída.
  90. </para>
  91. </sect2>
  92. <sect2 id="zend.view.introduction.options">
  93. <title>Options</title>
  94. <para>
  95. <classname>Zend_View</classname> has several options that may be set to
  96. configure the behaviour of your view scripts.
  97. </para>
  98. <itemizedlist>
  99. <listitem>
  100. <para>
  101. <property>basePath</property>: indicate a base path from which to set
  102. the script, helper, and filter path. It assumes a directory
  103. structure of:
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. base/path/
  107. helpers/
  108. filters/
  109. scripts/
  110. ]]></programlisting>
  111. <para>
  112. This may be set via <methodname>setBasePath()</methodname>,
  113. <methodname>addBasePath()</methodname>, or the <property>basePath</property>
  114. option to the constructor.
  115. </para>
  116. </listitem>
  117. <listitem>
  118. <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 UTF-8. May be set via
  123. <methodname>setEncoding()</methodname> or the <property>encoding</property>
  124. option to the constructor.
  125. </para>
  126. </listitem>
  127. <listitem>
  128. <para>
  129. <property>escape</property>: indicate a callback to be used by
  130. <methodname>escape()</methodname>. May be set via
  131. <methodname>setEscape()</methodname> or the <property>escape</property> option
  132. to the constructor.
  133. </para>
  134. </listitem>
  135. <listitem>
  136. <para>
  137. <property>filter</property>: indicate a filter to use after rendering
  138. a view script. May be set via <methodname>setFilter()</methodname>,
  139. <methodname>addFilter()</methodname>, or the <property>filter</property> option
  140. to the constructor.
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <property>strictVars</property>: force <classname>Zend_View</classname> to emit
  146. notices and warnings when uninitialized view variables are
  147. accessed. This may be set by calling
  148. <methodname>strictVars(true)</methodname> or passing the
  149. <property>strictVars</property> option to the constructor.
  150. </para>
  151. </listitem>
  152. </itemizedlist>
  153. </sect2>
  154. <sect2 id="zend.view.introduction.shortTags">
  155. <title>Short Tags with View Scripts</title>
  156. <para>
  157. In our examples, we make use of <acronym>PHP</acronym> long tags:
  158. <emphasis>&lt;?php</emphasis>. We also favor the use of <ulink
  159. url="http://us.php.net/manual/en/control-structures.alternative-syntax.php">alternate
  160. syntax for control structures</ulink>. These are convenient shorthands to use when
  161. writing view scripts, as they make the constructs more terse, keep statements on single
  162. lines, and eliminate the need to hunt for brackets within <acronym>HTML</acronym>.
  163. </para>
  164. <para>
  165. In previous versions, we often recommended using short tags (<emphasis>&lt;?</emphasis>
  166. and <emphasis>&lt;?=</emphasis>), as they make the view scripts slightly less verbose.
  167. However, the default for the <filename>php.ini</filename>
  168. <constant>short_open_tag</constant> setting is typically off in production or on shared
  169. hosts -- making their use not terribly portable. If you use template
  170. <acronym>XML</acronym> in view scripts, short open tags will cause the templates to fail
  171. validation. Finally, if you use short tags when <constant>short_open_tag</constant> is
  172. off, the view scripts will either cause errors or simply echo <acronym>PHP</acronym>
  173. code back to the viewer.
  174. </para>
  175. <para>
  176. If, despite these warnings, you wish to use short tags but they are disabled, you have
  177. two options:
  178. </para>
  179. <itemizedlist>
  180. <listitem>
  181. <para>
  182. Turn on short tags in your <filename>.htaccess</filename> file:
  183. </para>
  184. <programlisting language="apache"><![CDATA[
  185. php_value "short_open_tag" "on"
  186. ]]></programlisting>
  187. <para>
  188. This will only be possible if you are allowed to create and
  189. utilize <filename>.htaccess</filename> files. This directive can
  190. also be added to your <filename>httpd.conf</filename> file.
  191. </para>
  192. </listitem>
  193. <listitem>
  194. <para>
  195. Enable an optional stream wrapper to convert short tags to
  196. long tags on the fly:
  197. </para>
  198. <programlisting language="php"><![CDATA[
  199. $view->setUseStreamWrapper(true);
  200. ]]></programlisting>
  201. <para>
  202. This registers <classname>Zend_View_Stream</classname> as a stream
  203. wrapper for view scripts, and will ensure that your code
  204. continues to work as if short tags were enabled.
  205. </para>
  206. </listitem>
  207. </itemizedlist>
  208. <warning>
  209. <title>View Stream Wrapper Degrades Performance</title>
  210. <para>
  211. Usage of the stream wrapper <emphasis>will</emphasis> degrade
  212. performance of your application, though actual benchmarks are
  213. unavailable to quantify the amount of degradation. We recommend
  214. that you either enable short tags, convert your scripts to use
  215. full tags, or have a good partial and/or full page content
  216. caching strategy in place.
  217. </para>
  218. </warning>
  219. </sect2>
  220. <sect2 id="zend.view.introduction.accessors">
  221. <title>Utility Accessors</title>
  222. <para>
  223. Typically, you'll only ever need to call on <methodname>assign()</methodname>,
  224. <methodname>render()</methodname>, or one of the methods for setting/adding
  225. filter, helper, and script paths. However, if you wish to extend
  226. <classname>Zend_View</classname> yourself, or need access to some of its
  227. internals, a number of accessors exist:
  228. </para>
  229. <itemizedlist>
  230. <listitem>
  231. <para>
  232. <methodname>getVars()</methodname> will return all assigned variables.
  233. </para>
  234. </listitem>
  235. <listitem>
  236. <para>
  237. <methodname>clearVars()</methodname> will clear all assigned variables;
  238. useful when you wish to re-use a view object, but want to
  239. control what variables are available.
  240. </para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. <methodname>getScriptPath($script)</methodname> will retrieve the
  245. resolved path to a given view script.
  246. </para>
  247. </listitem>
  248. <listitem>
  249. <para>
  250. <methodname>getScriptPaths()</methodname> will retrieve all registered
  251. script paths.
  252. </para>
  253. </listitem>
  254. <listitem>
  255. <para>
  256. <methodname>getHelperPath($helper)</methodname> will retrieve the
  257. resolved path to the named helper class.
  258. </para>
  259. </listitem>
  260. <listitem>
  261. <para>
  262. <methodname>getHelperPaths()</methodname> will retrieve all registered
  263. helper paths.
  264. </para>
  265. </listitem>
  266. <listitem>
  267. <para>
  268. <methodname>getFilterPath($filter)</methodname> will retrieve the
  269. resolved path to the named filter class.
  270. </para>
  271. </listitem>
  272. <listitem>
  273. <para>
  274. <methodname>getFilterPaths()</methodname> will retrieve all registered
  275. filter paths.
  276. </para>
  277. </listitem>
  278. </itemizedlist>
  279. </sect2>
  280. </sect1>
  281. <!--
  282. vim:se ts=4 sw=4 et:
  283. -->