在Yii中整合Smarty

来源:互联网 发布:积分兑换软件 编辑:程序博客网 时间:2024/06/06 00:38

本文介绍Yii如何整合Smarty。

虽然说Yii的viewhelper已经够强大,CHtml,Jui..等等,甚至可以灵活的扩展这些组件,整合Smarty确实有点舍近求远。但是萝卜白菜,各有所爱,Yii灵活的扩展性,即插即用的Components,very gelivable..!也算抛砖引玉,整合其他第三方类库也是如此。

 

首先在protected中新建vendor文件夹或自定义的目录中放入我们的smarty类包,我这里是用vendor。

按照约定我们同在protected下的extensions(Yii的扩展默认都扔到这里)中建立CSmarty类文件。

 

内容如下:

file:webapp/protected/extensions/CSmarty.php

<?php

require_once(Yii::getPathOfAlias('application.vendor.smarty').DIRECTORY_SEPARATOR.'Smarty.class.php');
define('SMARTY_VIEW_DIR',Yii::getPathOfAlias('application.views.smarty'));

class CSmarty extends Smarty {
   
    const DS =DIRECTORY_SEPARATOR;
   
    function__construct() {
       parent::__construct();
       
       $this->template_dir =SMARTY_VIEW_DIR.self::DS.'tpl';
       $this->compile_dir =SMARTY_VIEW_DIR.self::DS.'tpl_c';
       $this->caching = true;
       $this->cache_dir =SMARTY_VIEW_DIR.self::DS.'cache';
       $this->left_delimiter  '<!--{';
       $this->right_delimiter = '}-->';
       $this->cache_lifetime = 3600;
    }

    functioninit() {}
   
}


?>

 

然后建立相关联的文件夹。

 

最后是我们的配置部分

打开protected/config/main.php

 

在components数组中加入

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

 

action:

Yii::app()->smarty;

 

如此就会得到smarty的实例了 : )