名称
Yaf_Dispatcher::setView
(Since Yaf 1.0.0.5)
参数
$view
-
一个实现了
Yaf_View_Interface
的视图引擎实例
例子
例 11.32. Yaf_Dispatcher::setView
的例子
<?php /** * 所有在Bootstrap类中, 以_init开头的方法, 都会被Yaf调用, * 这些方法, 都接受一个参数:Yaf_Dispatcher $dispatcher * 调用的次序, 和申明的次序相同 */ class Bootstrap extends Yaf_Bootstrap_Abstract{ /** * 自定义视图引擎 */ public function _initSmarty(Yaf_Dispatcher $dispatcher) { $smarty = new Smarty_Adapter(null, Yaf_Registry::get("config")->get("smarty")); Yaf_Dispatcher::getInstance()->setView($smarty); } } /** * 视图引擎定义 * Smarty/Adapter.php */ class Smarty_Adapter implements Yaf_View_Interface { /** * Smarty object * @var Smarty */ public $_smarty; /** * Constructor * * @param string $tmplPath * @param array $extraParams * @return void */ public function __construct($tmplPath = null, $extraParams = array()) { require "Smarty.class.php"; $this->_smarty = new Smarty; if (null !== $tmplPath) { $this->setScriptPath($tmplPath); } foreach ($extraParams as $key => $value) { $this->_smarty->$key = $value; } } /** * Assign variables to the template * * Allows setting a specific key to the specified value, OR passing * an array of key => value pairs to set en masse. * * @see __set() * @param string|array $spec The assignment strategy to use (key or * array of key => value pairs) * @param mixed $value (Optional) If assigning a named variable, * use this as the value. * @return void */ public function assign($spec, $value = null) { if (is_array($spec)) { $this->_smarty->assign($spec); return; } $this->_smarty->assign($spec, $value); } /** * Processes a template and returns the output. * * @param string $name The template to process. * @return string The output. */ public function render($name) { return $this->_smarty->fetch($name); } } ?>