Yii整合smarty

在protected下的extensions文件夹放入咱们的smarty类包,按照约定咱们同在protected下的extensions(Yii的扩展默认都扔到这里)中创建CSmarty类文件。php

内容以下:数组

  
  
           
  
  
  1. <?php  
  2. /**  
  3. *Author:Elite  
  4. */ 
  5.  
  6. require_once(Yii::getPathOfAlias('application.extensions.smarty').DIRECTORY_SEPARATOR.'Smarty.class.php');  
  7. define('SMARTY_VIEW_DIR', Yii::getPathOfAlias('application.views'));  
  8.  
  9. class CSmarty extends Smarty {  
  10.     const DIR_SEP = DIRECTORY_SEPARATOR;  
  11.     function __construct() {  
  12.         parent::__construct();  
  13.          
  14.         $this->template_dir = SMARTY_VIEW_DIR;  
  15.         $this->compile_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'template_c';  
  16.         $this->caching = true;  
  17.         $this->cache_dir = SMARTY_VIEW_DIR.self::DIR_SEP.'cache';  
  18.         $this->left_delimiter  =  '<!--{';  
  19.         $this->right_delimiter =  '}-->';  
  20.         $this->cache_lifetime = 3600;  
  21.     }  
  22.     function init() {}  
  23. }  
  24. ?> 

而后创建相关联的文件夹。app

紧接这是咱们的配置部分ide

打开protected/config/main.phpui

在components数组中加入this

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

最后在action中直接用Yii::app()->smarty就能够试用smarty了。若是每次在action中使用Yii::app()->smarty比较麻烦的话,能够在components下的Controller中能够加入component

protected $smarty = '';get

protected function init() {
       $this->smarty = Yii::app()->smarty;
 }string

而后在action中直接能够用$this->smarty使用smarty了。

相关文章
相关标签/搜索