yii整合smarty

来源:互联网 发布:薛蟠 知乎 编辑:程序博客网 时间:2024/06/05 22:30

1.在protected下新建extensions,在extensions文件夹,把smarty的libs文件夹下文件考入extensions并改名为smarty

2.在protected下的extensions(Yii的扩展默认都扔到这里)中建立CSmarty类文件。

<?php/** *Author:Elite */require_once(Yii::getPathOfAlias('application.extensions.smarty').DIRECTORY_SEPARATOR.'Smarty.class.php');define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views'));class CSmarty extends Smarty {const DIR_SEP = DIRECTORY_SEPARATOR;function __construct() {parent::__construct(); $this->template_dir = 'http://xxxxxx.cn/';//模版位置可在项目内SMARTY_VIEW_DIR也可以引用项目外$this->compile_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'template_c'; //生成文件存放位置$this->caching = false; //是否需要缓存//$this->cache_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'cache'; //缓存存放位置$this->left_delimiter  =  '{#';//标签开始$this->right_delimiter =  '#}';//标签结束$this->cache_lifetime = 3600;}function init() {Yii::registerAutoloader('smartyAutoload');//如不写 要在相应的文件夹下建立template_c文件夹和cache文件夹}}?>


3.打开protected/config/main.php,在components数组中加入

//smarty'smarty'=>array('class'=>'application.extensions.CSmarty',),

4.最后在action中直接用Yii::app()->smarty就可以试用smarty了。如果每次在action中使用Yii::app()->smarty比较麻烦的话,可以在components下的Controller中可以加入

public function actionTest() {$smarty= Yii::app()->smarty;$mburl='test.html';//模版名$smarty->smarty->assign('content','替换为');$smarty->smarty->display($mburl);}


原创粉丝点击