Yii 1.1: How to use Smarty renderer in Yii

7 followers

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

Total 2 comments

#3068 report it
dongbeta at 2011/03/13 12:28am
I know the Smarty renderer extension

I know the Smarty renderer extension, But it couldn't work.

#3067 report it
rosko at 2011/03/13 12:20am
Extension

Leave a comment

Please to leave your comment.

Write new article
  • Written by: dongbeta
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +4
  • Viewed: 16,888 times
  • Created on: Mar 12, 2011
  • Last updated: never
  • Tags: smarty, renderer