Home > PHP > Template Engine class

Template Engine class

December 16, 2009

Notice: This class is not for public usage and is subject to change.


<?php
/**
 * Class Template. A template engine class. Requires PHP 5+
 *
 * @package    		Template
 * @category   		Template
 * @author     		Costin Trifan 
 * @copyright  		2009 Costin Trifan
 * @licence    		Attribution-NonCommercial-NoDerivs 2.5	http://creativecommons.org/licenses/by-nc-nd/2.5/legalcode
 * @version    		1.0
 * @last-update		dec.2009
 *
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE").
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW. ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS
 * LICENSE OR COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE.
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS. 
 *
 *	http://creativecommons.org/licenses/by-nc-nd/2.5/legalcode
 */
class Template
{

	private function __clone(){}
	private function __sleep(){}
	private function __wakeup(){}

	/**
	 * RESTRICT ACCESS TO CLASS'S GLOBAL VARIABLES
	 (=============================================)
	 */

	public function __get( $name )
	{
		if (isset($this->_vars[$name]) and !empty($this->_vars[$name]))
		{
			return $this->_vars[ $name ];
		}
		else return null;
	}

	public function __set( $name, $value )
	{
		$this->_vars[$name] = $value;
	}



	private
		 $_tplDir = 'tpl/'		# The name of the folder where the template files are supposed to be stored.
		,$_vars = array()		# The class's variables array.
		,$_leftDel = '{'		# The left delimiter to use in the templates files to mark a template variable.
		,$_rightDel = '}'		# The right delimiter to use in the templates files to mark a template variable.
		,$_cacheObj = null		# Holds the instance of the ClibCache class
			;


	/**
	 * .ctor
	 */
	public function __construct( $tplDir = '', $cacheObj = null, $leftDelimiter = '{', $rightDelimiter = '}' )
	{
		if (isset($tplDir) and trim($tplDir) != '') {
			$this->_tplDir = $tplDir;
		}
		if (isset($cacheObj) and (! is_null($cacheObj)) ) {
			$this->_cacheObj = $cacheObj;
		}
		if (isset($leftDelimiter) and trim($leftDelimiter) != '{') {
			$this->_leftDel = $leftDelimiter;
		}
		if (isset($rightDelimiter) and trim($rightDelimiter) != '}') {
			$this->_rightDel = $rightDelimiter;
		}
	}



	/**
	* Add a variable to the vars array. This variable will be replaced in a template.
	*
	* param string $name  The name of the variable to store in the vars array.
	* param mixed $value  The value of the variable.
	* return $this
	*/
	public function SetVar( $name, $value )
	{
		$this->__set( $name, $value );
		return $this;
	}


	/**
	* Get a variable from the vars array.
	*
	* param string $name  The name of the variable to retrieve from the vars array.
	* return mixed
	*/
	public function GetVar( $name )
	{
		return $this->__get( $name );
	}


	/**
	* Delete all variables from the vars array.
	*
	* return $this
	*/
	public function ClearVars()
	{
		$this->_vars = array();
		return $this;
	}


	/**
	* Get all variables from the vars array.
	*
	* return array
	*/
	public function GetAllVars()
	{
		return $this->_vars;
	}



	/**
	* Retrieve the content of a template.
	*
	* param string $template  	The name of the template file to load.
	* param bool $getCached  	Whether or not to load the chached version of the $template
	* return string  The template's html content.
	*/
	function GetFile( $fileName, $getCached = false )
	{
		$data = '';

		$dir = ( $getCached ) ? $this->_cacheObj->GetCacheDir() : $this->_tplDir;

		if ($getCached) { $data = $this->_cacheObj->Get($dir.$fileName); }
		else {
			$file = $dir.$fileName;
			if ( file_exists($file) ) {
				$data = $this->Parse( $file );
			}
		}
		return $data;
	}


	/**
	* Outputs the template's html content.
	*
	* param string $fileName  The name of the template file to load.
	* param bool $getCached  	Whether or not to load the chached version of the $fileName
	* return $this
	*/
	function Display( $fileName, $getCached = false )
	{
		echo $this->GetFile( $fileName, $getCached );
		return $this;
	}




	/**
	* Replaces the variables from the specified template file.
	*
	* access protected
	* param string $file  The name of the template file to load.
	* return string  The template's parsed content.
	*/
	protected function Parse ( $file )
	{
		$content = '';

		ob_start();
			include_once $file;
			$content = ob_get_contents();
		ob_end_clean();

		if ($content == '') { return $content; }

		// make sure there is something to parse
		if ( ($pos = strpos($content, $this->_leftDel)) === FALSE ) { return $content; }

		if (count($this->_vars) > 0)
		{
			foreach($this->_vars as $name => $value)
			{
				if (is_string($value))
				{
					$var = $this->_leftDel. $name .$this->_rightDel;
					$content = str_ireplace($var, $value, $content);
				}
			}
		}
		return $content;
	}



	/**
	* Replaces the variables from the specified content block.
	*
	* access protected
	* param string $block  The content to be parsed.
	* return string  The parsed content.
	*/
	private function _parseBlock ( $data )
	{
		$content = '';

		if ($data == '') { return $content; }

		// make sure there is something to parse
		if ( ($pos = strpos($content, $this->_leftDel)) === FALSE ) { return $content; }

		if (count($this->_vars) > 0)
		{
			foreach($this->_vars as $name => $value)
			{
				if (is_string($value))
				{
					$var = $this->_leftDel. $name .$this->_rightDel;
					$content = str_ireplace($var, $value, $content);
				}
			}
		}
		return $content;
	}

}
/*[ end class ]*/
?>
Advertisements
Categories: PHP Tags: ,
%d bloggers like this: