©
本文档使用 PHP中文网手册 发布
(Yaf >=1.0.0)
Yaf_Dispatcher::setView — 设置视图引擎
$view
)如果你想使用自己的视图引擎代替 Yaf_View_Simple , 这个函数会帮你解决这个问题。
view
A Yaf_View_Interface instance
Example #1 A custom View engine() example
<?php
require "/path/to/smarty/Smarty.class.php" ;
class Smarty_Adapter implements Yaf_View_Interface
{
public $_smarty ;
public function __construct ( $tmplPath = null , $extraParams = array()) {
$this -> _smarty = new Smarty ;
if ( null !== $tmplPath ) {
$this -> setScriptPath ( $tmplPath );
}
foreach ( $extraParams as $key => $value ) {
$this -> _smarty -> $key = $value ;
}
}
public function setScriptPath ( $path )
{
if ( is_readable ( $path )) {
$this -> _smarty -> template_dir = $path ;
return;
}
throw new Exception ( 'Invalid path provided' );
}
public function __set ( $key , $val )
{
$this -> _smarty -> assign ( $key , $val );
}
public function __isset ( $key )
{
return ( null !== $this -> _smarty -> get_template_vars ( $key ));
}
public function __unset ( $key )
{
$this -> _smarty -> clear_assign ( $key );
}
public function assign ( $spec , $value = null ) {
if ( is_array ( $spec )) {
$this -> _smarty -> assign ( $spec );
return;
}
$this -> _smarty -> assign ( $spec , $value );
}
public function clearVars () {
$this -> _smarty -> clear_all_assign ();
}
public function render ( $name , $value = NULL ) {
return $this -> _smarty -> fetch ( $name );
}
public function display ( $name , $value = NULL ) {
echo $this -> _smarty -> fetch ( $name );
}
}
?>
Example #2 Yaf_Dispatcher::setView() example
<?php
class Bootstrap extends Yaf_Bootstrap_Abstract {
public function _initConfig () {
$config = Yaf_Application :: app ()-> getConfig ();
Yaf_Registry :: set ( "config" , $config );
}
public function _initLocalName () {
Yaf_Loader :: getInstance ()-> registerLocalNamespace ( 'Smarty' );
}
public function _initSmarty ( Yaf_Dispatcher $dispatcher ) {
$smarty = new Smarty_Adapter ( null , Yaf_Registry :: get ( "config" )-> get ( "smarty" ));
$dispatcher -> setView ( $smarty );
}
}
?>