smarty库放在extentsion目录下
按照约定我们同在protected下的extensions(Yii的扩展默认都扔到这里)中建立CSmarty类文件。
内容如下:
file:webapp/protected/extensions/CSmarty.php
_smarty = new Smarty();
$this->_smarty->template_dir = SMARTY_VIEW_DIR.DS.’tpl’;
$this->_smarty->compile_dir = SMARTY_VIEW_DIR.DS.’tpl_c’;
$this->_smarty->caching = false;
$this->_smarty->cache_dir = SMARTY_VIEW_DIR.DS.’cache’;
$this->_smarty->config_dir = SMARTY_VIEW_DIR.DS.’config’;
$this->_smarty->cache_lifetime = 3600;
}
}
?>
然后建立相关联的文件夹。
最后是我们的配置部分
打开protected/config/main.php
在components数组中加入
‘smarty’=>array(
‘class’=>’application.extensions.CSmarty’,
),
方案1:
action:
$smarty = Yii::app()->smarty;
$smarty->_smarty->display(‘test.tpl’);
方案2:
在\protected\components\Controller.php里加一个成员函数
function myRender (page, data=array(), engine=”)
{
if(‘smarty’=strtolower(engine){
$smarty = Yii::app()->smarty;
foreach($data as $key => $value){
$smarty->_smarty->assign( key , $value);
}
$smarty->_smarty->display( page );
}else{
$this->render(page, data);
}
}
action:
$this->myRender(‘test.tpl’);
这样使用方法,驱动可以任意切换。