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了。

相關文章
相關標籤/搜索