How to use Smarty renderer in Yii

You are viewing revision #1 of this wiki article.
This is the latest version of this article.

At first, create a class named SmartyRenderer under application/components or any directory you want.

<?php
class SmartyViewRenderer extends CApplicationComponent implements IViewRenderer
{
 
    /**
     * @var Smarty
     */
    private $_smarty;
    /**
     * @var string
     */
    public $fileExtension = '.html'; // or ".php" if you like
 
    public function init()
    {
        $smartyPath = Yii::getPathOfAlias('application.vendors.smarty');
        Yii::$classMap['Smarty'] = $smartyPath . '/Smarty.class.php';
        Yii::$classMap['Smarty_Internal_Data'] = $smartyPath . '/sysplugins/smarty_internal_data.php';
        $this->_smarty = new Smarty();
        $this->_smarty->compile_dir = Yii::app()->getRuntimePath() . '/smarty/compile';
        $this->_smarty->cache_dir = Yii::app()->getRuntimePath() . '/smarty/cache';
        $this->_smarty->left_delimiter = '{{'; // chenge it if you want other delimiter
        $this->_smarty->right_delimiter = '}}';
        $this->_smarty->force_compile = true;
        Yii::registerAutoloader('smartyAutoload');
    }
 
    public function renderFile($context, $file, $data, $return)
    {
        foreach ($data as $key => $value)
            $this->_smarty->assign($key, $value);
        $return = $this->_smarty->fetch($file);
        if ($return)
            return $return;
        else
            echo $return;
    }
}

Then, put smarty under appliction/vendors.

Now, the smarty renderer has been setup.

enjoy it.

[zh-cn] [如何在 Yii 1.x 中使用 Smarty 3](http://dongbeta.com/post/1215 "http://dongbeta.com/post/1215")

3 0
7 followers
Viewed: 22 860 times
Version: Unknown (update)
Category: How-tos
Written by: dongbeta
Last updated by: dongbeta
Created on: Mar 13, 2011
Last updated: 13 years ago
Update Article

Revisions

View all history