Yii1.1整合smarty模板
法一:
一,下载smarty3.X到本地,放到Yii框架 /protected/extensions/目录下
这里多余的smarty包里的demo和非重要文件都没有删除。
***要非常注意Smarty.class.php 的位置,这里有对其他php类文件的路径定义变量,如果改变了Smarty.class.php 位置,记得一定要修改该文件中路径变量,否则再引入sysplugins包中的php时 会报:找不到资源的错误!!***
二,在/protected/extensions/目录下创建CSmarty.php文件
<?php
define('SMARTY_SPL_AUTOLOAD', true);
require_once (Yii::getPathOfAlias('application.extensions.smarty').DIRECTORY_SEPARATOR.'libs'.DIRECTORY_SEPARATOR.'Smarty.class.php'); //smarty配置
define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views.sblog.smarty'));
class CSmarty extends Smarty{
public $_smarty;
function __construct() {
parent::__construct();
$this->template_dir = SMARTY_VIEW_DIR.DS.'tpl';
$this->compile_dir = SMARTY_VIEW_DIR.DS.'tpl_c';
$this->caching = false;
$this->cache_dir = SMARTY_VIEW_DIR.DS.'cache';
$this->config_dir = SMARTY_VIEW_DIR.DS.'config';
$this->cache_lifetime = 3600;
}
function init(){
// Yii::registerAutoloader('smartyAutoload');
}
}
?>
注意两点:
1,Smarty.class.php的路径,根据自己的实际情况来写
2,SMARTY_VIEW_DIR 这个是smarty的模板起作用的路径,可以随意指定。
3,DS 是路径分隔符’/’
三,在/protected/config/main.php 文件中加
'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'smarty'=>array(
'class'=>'application.extensions.CSmarty',
),
这样就把smarty整合到Yii1.1里了。
如何使用呢?
一,在/protected/components/Controller.php 文件中,加
public $smarty;
public function init() {
$this->smarty = Yii::app()->smarty;
}
二,在/protected/controllers/XXXController.php中的actionXXX函数中
$world = "lily";
$this->smarty->assign( 'world', $world );
$this->smarty->display( 'test.html');
test.html 在smarty模板目录中建立。