1 <sect1 id="zend.view.scripts">
6 Controller完成变量赋值和调用render()之后,Zend_View就会调用视图脚本并在Zend_View的实例内部执行。因此,在你的视图脚本内,$this指向Zend_View的实例。
10 从控制器传递到视图的变量以Zend_View实例属性的形式来调用。例如,控制器有一个变量"something" ,那么视图代码中就要用$this->something来调用。这样的作法可以让你分清哪些是来自Zend_View实例的变量,哪些是视图自身的变量。
17 <programlisting role="php"><![CDATA[<?php if ($this->books): ?>
19 <!-- A table of some books. -->
26 <?php foreach ($this->books as $key => $val): ?>
28 <td><?php echo $this->escape($val['author']) ?></td>
29 <td><?php echo $this->escape($val['title']) ?></td>
37 <p>There are no books to display.</p>
42 <sect2 id="zend.view.scripts.escaping">
44 <title>转义输出(Escaping Output)</title>
47 View脚本的最重要的工作之一是保证输出的内容是合适的,比如需要避免跨站攻击漏洞(XSS)。除非你已经使用一个函数、类方法或助手类(helper)来转义内容,你需要在输出时对变量进行转义。
51 Zend_View带有一个escape()方法来提供这个功能:
54 <programlisting role="php"><![CDATA[<?php
59 echo $this->escape($this->variable);
64 默认地,escape()方法使用PHP函数htmlspecialchars()来过滤,但你也可以通过setEscape()方法来在Controller内告诉Zend_View需要怎么过滤。
67 <programlisting role="php"><![CDATA[<?php
69 $view = new Zend_View();
71 //设定要使用的转义回调函数(callback)
72 $view->setEscape('htmlentities');
75 $view->setEscape(array('SomeClass', 'methodName'));
78 $obj = new SomeClass();
79 $view->setEscape(array($obj, 'methodName'));
82 echo $view->render(...);
88 设定的转义函数会将需要转义的变量作为其第一个参数,其它参数是可选的。
93 <sect2 id="zend.view.scripts.templates">
97 尽管许多开发者觉得PHP本身就是一个强大的模板系统,但对模板设计师来说,使用PHP标签过于复杂。Zend_View提供了两套机制来同时满足这两种要求,一种是直接通过通过视图脚本,另一种是实现Zend_View_Interface接口。
100 <sect3 id="zend.view.scripts.templates.scripts">
101 <title>使用View脚本的模板系统</title>
104 View脚本可能要被用来初始化和操作一个其它模板对象的实例,例如PHPLIB风格的模板。这时View脚本可能是这样的:
107 <programlisting role="php"><![CDATA[<?php
108 include_once 'template.inc';
109 $tpl = new Template();
113 "booklist" => "booklist.tpl",
114 "eachbook" => "eachbook.tpl",
117 foreach ($this->books as $key => $val) {
118 $tpl->set_var('author', $this->escape($val['author']);
119 $tpl->set_var('title', $this->escape($val['title']);
120 $tpl->parse("books", "eachbook", true);
123 $tpl->pparse("output", "booklist");
125 $tpl->setFile("nobooks", "nobooks.tpl")
126 $tpl->pparse("output", "nobooks");
134 <programlisting role="html"><![CDATA[
135 <!-- booklist.tpl -->
144 <!-- eachbook.tpl -->
151 <p>There are no books to display.</p>]]>>
156 <sect3 id="zend.view.scripts.templates.interface">
157 <title>通过Zend_View_Interface接口使用模板系统</title>
161 实现一个与Zend_View兼容的模板系统是很简单的。你只需要实现<code>Zend_View_Interface</code>接口即可,该接口定义了要实现兼容的最低要求。
164 <programlisting role="php"><![CDATA[
166 * Return the actual template engine object
169 public function getEngine();
172 * Set the path to view scripts/templates
175 public function setScriptPath($path);
178 * Set a base path to all view resources
181 public function setBasePath($path, $prefix = 'Zend_View');
184 * Add an additional base path to view resources
187 public function addBasePath($path, $prefix = 'Zend_View');
190 * Retrieve the current script paths
193 public function getScriptPaths();
196 * Overloading methods for assigning template variables as object properties
197 * 重载方法,用于将赋值给模板变量,以对象属性的形式
199 public function __set($key, $value);
200 public function __get($key);
201 public function __isset($key);
202 public function __unset($key);
205 * Manual assignment of template variables, or ability to assign multiple
206 * variables en masse.
207 * 手动设置模板变量,或者一次赋值多个变量的功能
209 public function assign($spec, $value = null);
212 * Unset all assigned template variables
215 public function clearVars();
218 * Render the template named $name
221 public function render($name);]]>
225 使用这个接口,把第三方的模板系统封装成Zend_View兼容的类是相当容易的。例如,下面是封装Smarty的示例代码:
228 <programlisting role="php"><![CDATA[
229 require_once 'Zend/View/Interface.php';
230 require_once 'Smarty.class.php';
232 class Zend_View_Smarty implements Zend_View_Interface
243 * @param string $tmplPath
244 * @param array $extraParams
247 public function __construct($tmplPath = null, $extraParams = array())
249 $this->_smarty = new Smarty;
251 if (null !== $tmplPath) {
252 $this->setScriptPath($tmplPath);
255 foreach ($extraParams as $key => $value) {
256 $this->_smarty->$key = $value;
261 * Return the template engine object
265 public function getEngine()
267 return $this->_smarty;
271 * Set the path to the templates
273 * @param string $path The directory to set as the path.
276 public function setScriptPath($path)
278 if (is_readable($path)) {
279 $this->_smarty->template_dir = $path;
283 throw new Exception('Invalid path provided');
287 * Retrieve the current template directory
291 public function getScriptPaths()
293 return array($this->_smarty->template_dir);
297 * Alias for setScriptPath
299 * @param string $path
300 * @param string $prefix Unused
303 public function setBasePath($path, $prefix = 'Zend_View')
305 return $this->setScriptPath($path);
309 * Alias for setScriptPath
311 * @param string $path
312 * @param string $prefix Unused
315 public function addBasePath($path, $prefix = 'Zend_View')
317 return $this->setScriptPath($path);
321 * Assign a variable to the template
323 * @param string $key The variable name.
324 * @param mixed $val The variable value.
327 public function __set($key, $val)
329 $this->_smarty->assign($key, $val);
333 * Retrieve an assigned variable
335 * @param string $key The variable name.
336 * @return mixed The variable value.
338 public function __get($key)
340 return $this->_smarty->get_template_vars($key);
344 * Allows testing with empty() and isset() to work
349 public function __isset($key)
351 return (null !== $this->_smarty->get_template_vars($key));
355 * Allows unset() on object properties to work
360 public function __unset($key)
362 $this->_smarty->clear_assign($key);
366 * Assign variables to the template
368 * Allows setting a specific key to the specified value, OR passing an array
369 * of key => value pairs to set en masse.
372 * @param string|array $spec The assignment strategy to use (key or array of key
374 * @param mixed $value (Optional) If assigning a named variable, use this
378 public function assign($spec, $value = null)
380 if (is_array($spec)) {
381 $this->_smarty->assign($spec);
385 $this->_smarty->assign($spec, $value);
389 * Clear all assigned variables
391 * Clears all variables assigned to Zend_View either via {@link assign()} or
392 * property overloading ({@link __get()}/{@link __set()}).
396 public function clearVars()
398 $this->_smarty->clear_all_assign();
402 * Processes a template and returns the output.
404 * @param string $name The template to process.
405 * @return string The output.
407 public function render($name)
409 return $this->_smarty->fetch($name);
415 在这个示例中,实例化<code>Zend_View_Smarty</code>而不是<code>Zend_View</code>,然后就像使用 <code>Zend_View</code>一样地使用它。
418 <programlisting role="php"><![CDATA[
419 $view = new Zend_View_Smarty();
420 $view->setScriptPath('/path/to/templates');
421 $view->book = 'Zend PHP 5 Certification Study Guide';
422 $view->author = 'Davey Shafik and Ben Ramsey'
423 $rendered = $view->render('bookinfo.tpl');]]>