Update SmartyViewRenderer

Hi, I want to share about smarty view renderer that made by samdark (thanks to samdark) :slight_smile:


but when I use it, it have a bug I thing, the variable that will used to be displayed in smarty template must pass or declared through controller.


in protected/components/Controller.php

Class Controller extends CController {

public $title;

public $body;

// etc…


then after we declare variable in controller we can use it in smarty template with:

{$this->title} or {$this->body}

it is works fine, but I thing it is not simple way to implement. because we must declared some var before.

I have made some change into SmartyViewRenderer to get more easly in implementation.

so my SmartyViewRenderer Script changed into like this



 * Smarty renderer for Yii


 * Copy latest version of Smarty (libs contents) to vendors/Smarty/.


 * Add the following to your config file 'components' section:


 * 'viewRenderer'=>array(

 *     'class'=>'application.extensions.Smarty.CSmartyViewRenderer',

 *     'fileExtension' => '.tpl',

 *     //'pluginsDir' => 'application.smartyPlugins',

 *     //'configDir' => 'application.smartyConfig',

 *  ),


 * @author Alexander Makarov <sam@rmcreative.ru>

 * @link http://www.yiiframework.com/

 * @link http://www.smarty.net/


 * @version 0.9


class CSmartyViewRenderer extends CApplicationComponent implements IViewRenderer {

    public $fileExtension='.html';

    public $filePermission=0755;

    public $pluginsDir = null;

    public $configDir = null;

    private $smarty;


     * Component initialization


    function init(){



        $this->smarty = new Smarty();


        $this->smarty->template_dir = Yii::app()->getViewPath();


        $compileDir = Yii::app()->getRuntimePath().'/smarty/compiled/';


        // create compiled directory if not exists


            mkdir($compileDir, $this->filePermission, true);



        $this->smarty->compile_dir = $compileDir;



        $this->smarty->plugins_dir[] = Yii::getPathOfAlias('application.vendors.Smarty.plugins');


            $this->smarty->plugins_dir[] = Yii::getPathOfAlias($this->pluginsDir);



            $this->smarty->config_dir = Yii::getPathOfAlias($this->configDir);



        $this->smarty->assign("MEMORY",round(Yii::getLogger()->getMemoryUsage()/(1024*1024),2)." MB");


//I add assign function

        public function assign($k,$v){




         * Renders a view file.

         * This method is required by {@link IViewRenderer}.

         * @param CBaseController the controller or widget who is rendering the view file.

         * @param string the view file path

         * @param mixed the data to be passed to the view

         * @param boolean whether the rendering result should be returned

         * @return mixed the rendering result, or null if the rendering result is not needed.


        public function renderFile($context,$sourceFile,$data,$return) {

        // current controller properties will be accessible as {this.property}


        //$data['this'] = $context;

        // check if view file exists

        if(!is_file($sourceFile) || ($file=realpath($sourceFile))===false)

            throw new CException(Yii::t('yii','View file "{file}" does not exist.', array('{file}'=>$sourceFile)));


        //assign data

        // ini ternyata masalahnya kalo dipake maka untuk akses variabel menggunakan $this


        //$this->smarty->_tpl_vars = $data;

        $this->assign('content',$data['content']); //and I add this script

        $this->assign('this',$context); //and I add this script

        return $this->smarty->fetch($sourceFile);



so to use this further. eg: in your action

public function actionIndex(){

   Yii::app()->getViewRenderer()->assign('title',"My Title");



so in view file: you just write {$title} to display

I hope will be useful for everyone.

thank for all and sorry about my english if hard to understand <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/biggrin.gif' class='bbc_emoticon' alt=':D' />

Why don’t use $this->render(‘index’, array(‘title’ => ‘My title’))?

I have try that, but when I use {debug} it seem not work, the available variable only {$this} and {$content} when I debug this.