Zend_View-Scripts.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <sect1 id="zend.view.scripts">
  2. <title>视图脚本</title>
  3. <para>
  4. Controller完成变量赋值和调用render()之后,Zend_View就会调用视图脚本并在Zend_View的实例内部执行。因此,在你的视图脚本内,$this指向Zend_View的实例。
  5. </para>
  6. <para>
  7. 从控制器传递到视图的变量以Zend_View实例属性的形式来调用。例如,控制器有一个变量"something" ,那么视图代码中就要用$this->something来调用。这样的作法可以让你分清哪些是来自Zend_View实例的变量,哪些是视图自身的变量。
  8. </para>
  9. <para>
  10. 为了说明,这里有一个例子:
  11. </para>
  12. <programlisting role="php"><![CDATA[<?php if ($this->books): ?>
  13. <!-- A table of some books. -->
  14. <table>
  15. <tr>
  16. <th>Author</th>
  17. <th>Title</th>
  18. </tr>
  19. <?php foreach ($this->books as $key => $val): ?>
  20. <tr>
  21. <td><?php echo $this->escape($val['author']) ?></td>
  22. <td><?php echo $this->escape($val['title']) ?></td>
  23. </tr>
  24. <?php endforeach; ?>
  25. </table>
  26. <?php else: ?>
  27. <p>There are no books to display.</p>
  28. <?php endif; ]]>
  29. </programlisting>
  30. <sect2 id="zend.view.scripts.escaping">
  31. <title>转义输出(Escaping Output)</title>
  32. <para>
  33. View脚本的最重要的工作之一是保证输出的内容是合适的,比如需要避免跨站攻击漏洞(XSS)。除非你已经使用一个函数、类方法或助手类(helper)来转义内容,你需要在输出时对变量进行转义。
  34. </para>
  35. <para>
  36. Zend_View带有一个escape()方法来提供这个功能:
  37. </para>
  38. <programlisting role="php"><![CDATA[<?php
  39. // 不好的做法:
  40. echo $this->variable;
  41. // 好的做法:
  42. echo $this->escape($this->variable);
  43. ]]>
  44. </programlisting>
  45. <para>
  46. 默认地,escape()方法使用PHP函数htmlspecialchars()来过滤,但你也可以通过setEscape()方法来在Controller内告诉Zend_View需要怎么过滤。
  47. </para>
  48. <programlisting role="php"><![CDATA[<?php
  49. //创建一个Zend_View实例
  50. $view = new Zend_View();
  51. //设定要使用的转义回调函数(callback)
  52. $view->setEscape('htmlentities');
  53. //或者使用一个静态类方法作为回调函数
  54. $view->setEscape(array('SomeClass', 'methodName'));
  55. //或者是一个对象实例的类方法
  56. $obj = new SomeClass();
  57. $view->setEscape(array($obj, 'methodName'));
  58. //最后输出你的视图
  59. echo $view->render(...);
  60. ]]>
  61. </programlisting>
  62. <para>
  63. 设定的转义函数会将需要转义的变量作为其第一个参数,其它参数是可选的。
  64. </para>
  65. </sect2>
  66. <sect2 id="zend.view.scripts.templates">
  67. <title>使用模板系统</title>
  68. <para>
  69. 尽管许多开发者觉得PHP本身就是一个强大的模板系统,但对模板设计师来说,使用PHP标签过于复杂。Zend_View提供了两套机制来同时满足这两种要求,一种是直接通过通过视图脚本,另一种是实现Zend_View_Interface接口。
  70. </para>
  71. <sect3 id="zend.view.scripts.templates.scripts">
  72. <title>使用View脚本的模板系统</title>
  73. <para>
  74. View脚本可能要被用来初始化和操作一个其它模板对象的实例,例如PHPLIB风格的模板。这时View脚本可能是这样的:
  75. </para>
  76. <programlisting role="php"><![CDATA[<?php
  77. include_once 'template.inc';
  78. $tpl = new Template();
  79. if ($this->books) {
  80. $tpl->setFile(array(
  81. "booklist" => "booklist.tpl",
  82. "eachbook" => "eachbook.tpl",
  83. ));
  84. foreach ($this->books as $key => $val) {
  85. $tpl->set_var('author', $this->escape($val['author']);
  86. $tpl->set_var('title', $this->escape($val['title']);
  87. $tpl->parse("books", "eachbook", true);
  88. }
  89. $tpl->pparse("output", "booklist");
  90. } else {
  91. $tpl->setFile("nobooks", "nobooks.tpl")
  92. $tpl->pparse("output", "nobooks");
  93. }
  94. ]]></programlisting>
  95. <para>
  96. 下面是相关的模板文件:
  97. </para>
  98. <programlisting role="html"><![CDATA[
  99. <!-- booklist.tpl -->
  100. <table>
  101. <tr>
  102. <th>Author</th>
  103. <th>Title</th>
  104. </tr>
  105. {books}
  106. </table>
  107. <!-- eachbook.tpl -->
  108. <tr>
  109. <td>{author}</td>
  110. <td>{title}</td>
  111. </tr>
  112. <!-- nobooks.tpl -->
  113. <p>There are no books to display.</p>]]>>
  114. </programlisting>
  115. </sect3>
  116. <sect3 id="zend.view.scripts.templates.interface">
  117. <title>通过Zend_View_Interface接口使用模板系统</title>
  118. <para>
  119. 实现一个与Zend_View兼容的模板系统是很简单的。你只需要实现<code>Zend_View_Interface</code>接口即可,该接口定义了要实现兼容的最低要求。
  120. </para>
  121. <programlisting role="php"><![CDATA[
  122. /**
  123. * Return the actual template engine object
  124. * 返回实际模板系统的对象
  125. */
  126. public function getEngine();
  127. /**
  128. * Set the path to view scripts/templates
  129. * 设置视图脚本/模板的路径
  130. */
  131. public function setScriptPath($path);
  132. /**
  133. * Set a base path to all view resources
  134. * 给所有视图资源设置基本路径
  135. */
  136. public function setBasePath($path, $prefix = 'Zend_View');
  137. /**
  138. * Add an additional base path to view resources
  139. * 给视图资源添加另外的基本路径
  140. */
  141. public function addBasePath($path, $prefix = 'Zend_View');
  142. /**
  143. * Retrieve the current script paths
  144. * 获取当前脚本路径
  145. */
  146. public function getScriptPaths();
  147. /**
  148. * Overloading methods for assigning template variables as object properties
  149. * 重载方法,用于将赋值给模板变量,以对象属性的形式
  150. */
  151. public function __set($key, $value);
  152. public function __get($key);
  153. public function __isset($key);
  154. public function __unset($key);
  155. /**
  156. * Manual assignment of template variables, or ability to assign multiple
  157. * variables en masse.
  158. * 手动设置模板变量,或者一次赋值多个变量的功能
  159. */
  160. public function assign($spec, $value = null);
  161. /**
  162. * Unset all assigned template variables
  163. * 消除所有已赋值的变量
  164. */
  165. public function clearVars();
  166. /**
  167. * Render the template named $name
  168. * 输出参数$name指定的某个模板
  169. */
  170. public function render($name);]]>
  171. </programlisting>
  172. <para>
  173. 使用这个接口,把第三方的模板系统封装成Zend_View兼容的类是相当容易的。例如,下面是封装Smarty的示例代码:
  174. </para>
  175. <programlisting role="php"><![CDATA[
  176. require_once 'Zend/View/Interface.php';
  177. require_once 'Smarty.class.php';
  178. class Zend_View_Smarty implements Zend_View_Interface
  179. {
  180. /**
  181. * Smarty object
  182. * @var Smarty
  183. */
  184. protected $_smarty;
  185. /**
  186. * Constructor
  187. *
  188. * @param string $tmplPath
  189. * @param array $extraParams
  190. * @return void
  191. */
  192. public function __construct($tmplPath = null, $extraParams = array())
  193. {
  194. $this->_smarty = new Smarty;
  195. if (null !== $tmplPath) {
  196. $this->setScriptPath($tmplPath);
  197. }
  198. foreach ($extraParams as $key => $value) {
  199. $this->_smarty->$key = $value;
  200. }
  201. }
  202. /**
  203. * Return the template engine object
  204. *
  205. * @return Smarty
  206. */
  207. public function getEngine()
  208. {
  209. return $this->_smarty;
  210. }
  211. /**
  212. * Set the path to the templates
  213. *
  214. * @param string $path The directory to set as the path.
  215. * @return void
  216. */
  217. public function setScriptPath($path)
  218. {
  219. if (is_readable($path)) {
  220. $this->_smarty->template_dir = $path;
  221. return;
  222. }
  223. throw new Exception('Invalid path provided');
  224. }
  225. /**
  226. * Retrieve the current template directory
  227. *
  228. * @return string
  229. */
  230. public function getScriptPaths()
  231. {
  232. return array($this->_smarty->template_dir);
  233. }
  234. /**
  235. * Alias for setScriptPath
  236. *
  237. * @param string $path
  238. * @param string $prefix Unused
  239. * @return void
  240. */
  241. public function setBasePath($path, $prefix = 'Zend_View')
  242. {
  243. return $this->setScriptPath($path);
  244. }
  245. /**
  246. * Alias for setScriptPath
  247. *
  248. * @param string $path
  249. * @param string $prefix Unused
  250. * @return void
  251. */
  252. public function addBasePath($path, $prefix = 'Zend_View')
  253. {
  254. return $this->setScriptPath($path);
  255. }
  256. /**
  257. * Assign a variable to the template
  258. *
  259. * @param string $key The variable name.
  260. * @param mixed $val The variable value.
  261. * @return void
  262. */
  263. public function __set($key, $val)
  264. {
  265. $this->_smarty->assign($key, $val);
  266. }
  267. /**
  268. * Retrieve an assigned variable
  269. *
  270. * @param string $key The variable name.
  271. * @return mixed The variable value.
  272. */
  273. public function __get($key)
  274. {
  275. return $this->_smarty->get_template_vars($key);
  276. }
  277. /**
  278. * Allows testing with empty() and isset() to work
  279. *
  280. * @param string $key
  281. * @return boolean
  282. */
  283. public function __isset($key)
  284. {
  285. return (null !== $this->_smarty->get_template_vars($key));
  286. }
  287. /**
  288. * Allows unset() on object properties to work
  289. *
  290. * @param string $key
  291. * @return void
  292. */
  293. public function __unset($key)
  294. {
  295. $this->_smarty->clear_assign($key);
  296. }
  297. /**
  298. * Assign variables to the template
  299. *
  300. * Allows setting a specific key to the specified value, OR passing an array
  301. * of key => value pairs to set en masse.
  302. *
  303. * @see __set()
  304. * @param string|array $spec The assignment strategy to use (key or array of key
  305. * => value pairs)
  306. * @param mixed $value (Optional) If assigning a named variable, use this
  307. * as the value.
  308. * @return void
  309. */
  310. public function assign($spec, $value = null)
  311. {
  312. if (is_array($spec)) {
  313. $this->_smarty->assign($spec);
  314. return;
  315. }
  316. $this->_smarty->assign($spec, $value);
  317. }
  318. /**
  319. * Clear all assigned variables
  320. *
  321. * Clears all variables assigned to Zend_View either via {@link assign()} or
  322. * property overloading ({@link __get()}/{@link __set()}).
  323. *
  324. * @return void
  325. */
  326. public function clearVars()
  327. {
  328. $this->_smarty->clear_all_assign();
  329. }
  330. /**
  331. * Processes a template and returns the output.
  332. *
  333. * @param string $name The template to process.
  334. * @return string The output.
  335. */
  336. public function render($name)
  337. {
  338. return $this->_smarty->fetch($name);
  339. }
  340. }]]>
  341. </programlisting>
  342. <para>
  343. 在这个示例中,实例化<code>Zend_View_Smarty</code>而不是<code>Zend_View</code>,然后就像使用 <code>Zend_View</code>一样地使用它。
  344. </para>
  345. <programlisting role="php"><![CDATA[
  346. $view = new Zend_View_Smarty();
  347. $view->setScriptPath('/path/to/templates');
  348. $view->book = 'Zend PHP 5 Certification Study Guide';
  349. $view->author = 'Davey Shafik and Ben Ramsey'
  350. $rendered = $view->render('bookinfo.tpl');]]>
  351. </programlisting>
  352. </sect3>
  353. </sect2>
  354. </sect1>
  355. <!--
  356. vim:se ts=4 sw=4 et:
  357. -->