ITeye博客

Yii整合smarty

yd514 · 2013-08-30 16:13 · Yii · Web前端 · Markdown

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

内容如下:

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

 

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

 

紧接这是我们的配置部分

打开protected/config/main.php

在components数组中加入

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

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

protected $smarty = '';

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

然后在action中直接可以用$this->smarty使用smarty了。

本文出自 “我相信” 博客,请务必保留此出处http://mrcelite.blog.51cto.com/2977858/789406

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。