<?php
define('TEMPLATE_EXT','.phtml');
/*
* Renders template
* Usage for handle templates
*/
class CtrlTemplate extends CtrlComponent
{
	public $template_dir = '';
	private $_vars = array(); // template vars
	private $_class_file;// class file name
	private $_phtml_file;// template php file name
	
	/*
	* Constructor
	* Sets the phtml file name
	* @parameter template file name
	*/
	public function __construct($object)
	{
		$file = get_class($object);
		$this->_class_file = $file.CLASS_EXT;// view file
		$this->_phtml_file = $file.TEMPLATE_EXT; // template phtml file name
	}
	/*
	* Sets variable to the template
	* @parameter variable name
	* @parameter variable value
	*/
	public function set($var,$value)
	{
		$val = $this->Session->get($var);
		if($val!=$value)
		{
			$this->Session->set($var,$value);			
		}
		$this->_vars[$var] = $value;
	}
	/*
	* Renders the content
	* Includes the template file
	* @returns string content of the template file
	*/
	public function render()
	{
		// Start output bufferin	
		$this->Response->start();													
		// check is the template file exists
		
		if(is_file_exists($this->template_dir.DIRECTORY_SEPARATOR.$this->_phtml_file) == false)
		{
			trigger_error("Can't locate <b>$this->_phtml_file file</b>", E_USER_ERROR);
			return false;
		}
		extract($this->_vars);	// extract vars to local namespace
		include($this->_phtml_file); // include the template file
		
		$content = $this->Response->getContent();// Get the contents of the buffer
		
		$this->Response->end();				// End buffering and discard
		
		return $content;					// return the contents		
	}
}
?>
