Haanga templates

Maybe this will help to some one…

Wanted to create an extension, but decided that Haanga not that flexible and I don’t know Yii that good yet…

Download haanga, and put it in vendorts under Haanga directory

in main config



	     'fileExtension' => '.html',

	     'options' => array(



In extension



 * Haanga view renderer


 * @author David Kurushin

 * @link http://haanga.org/


 * @version 1.0.4


class EHaangaViewRenderer extends CApplicationComponent implements IViewRenderer {


	 * The templete files extension

	 * @var string the extension


    public $fileExtension='.html';


	 * @var array Haanga options

	 * @see https://github.com/crodas/Haanga


	public $options = array();


     * Component initialization


    function init(){



        // need this since Yii autoload handler raises an error if class is not found


        // registering twig autoload handler

        require_once 'Haanga/lib/Haanga.php"';


        // adding back Yii autoload handler


        // setting cache path to application runtime directory

		$config['cache_dir'] = Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'views_haanga'.DIRECTORY_SEPARATOR;

		$config['template_dir'] = Yii::app()->getBasePath();

        // here we are using twig loader


        $loader = new Twig_Loader_Filesystem($this->getBasePath());

        $this->twig = new Twig_Environment($loader, $this->options);





	 * 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('yiiext','View file "{file}" does not exist.', array('{file}'=>$sourceFile)));

		$sourceFile = substr($sourceFile, strlen($this->getBasePath()));

		return Haanga::Load($sourceFile, $data);



	 * Theme-aware basepath

	 * @return string


	protected function getBasePath()


		if(Yii::app()->theme == null)

			return Yii::app()->getBasePath();

		return Yii::app()->theme->getBasePath();



I think Haanga the best template engine in terms of performance and yet functions…