Location: PHPKode > scripts > AbsTemplate > abstemplate/class.AbsTemplate.php
<?php
/**
* class AbsTemplate
*
* The Template Engine's base class.
*
* Features:
*     - set your own custom delimiters for variables to use inside template files,
*     - use any type of templates you want, that is, the template files can have any extension you want(be it .php, .inc, .tpl, etc...),
*     - display multiple templates per page,
*     - cache templates,
*     - assign the content of a template to a variable and, when appropriate, just display its content.
* 
* @package    AbsTemplate
* @category   Cache, Templates
* @author     Costin Trifan <hide@address.com>
* @copyright  2009 Costin Trifan
* @licence    MIT License http://en.wikipedia.org/wiki/MIT_License
* @version    1.0
* 
* Copyright (c) 2009 Costin Trifan <http://june-js.com/>
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
class AbsTemplate 
{
	private function __clone(){}

# PROTECTED PROPERTIES
#======================

	protected 
		$tpl_dir = '',			# The name of the folder where the template files are supposed to be stored.
		$cache_dir = '',		# The name of the folder where the cached template files are supposed to be stored.
		$left_delimiter = '{',	# The left delimiter to use in the templates files to mark a template variable.
		$right_delimiter = '}',	# The right delimiter to use in the templates files to mark a template variable.
		$vars = array();		# The class's variables array.


# PUBLIC METHODS
#======================

	/**
	* Constructor. Setup class's variables.
	*
	* @param string $tplDir  The path to the templates folder.
	* @param string $cacheDir  The path to the folder that stores the cached files
	* @param string $lDelim  The left delimiter to use in the templates files to mark a template variable.
	* @param string $rDelim  The right delimiter to use in the templates files to mark a template variable.
	* @return void
	*/
	public function __construct( $tplDir, $cacheDir, $lDelim='', $rDelim='' )
	{
		$this->SetTemplatesDirectory($tplDir);
		$this->SetCacheDirectory($cacheDir);
		$this->SetLeftDelimiter($lDelim);
		$this->SetRightDelimiter($rDelim);
	}

	/**
	* Set the path to the templates directory.
	*
	* @param string $tplDir  The path to the templates folder.
	* @return void
	*/
	public function SetTemplatesDirectory( $tplDir )
	{
		if (empty($tplDir))
			exit('Error in <strong>'.__CLASS__.'::SetTemplatesDirectory</strong> function. The path to the template directory has not been set!');

		if (@is_dir($tplDir)) $this->tpl_dir = $tplDir;
		else exit('The specified template directory does not exists!');
	}

	/**
	* Set the path to the templates' cache directory.
	*
	* @param string $cacheDir  The path to the templates' cache directory.
	* @return void
	*/
	public function SetCacheDirectory( $cacheDir )
	{
		if (empty($cacheDir))
			exit('Error in <strong>'.__CLASS__.'::SetCacheDirectory</strong> function. The path to the templates\' cache directory has not been set!');
		
		if (@is_dir($cacheDir)) $this->cache_dir = $cacheDir;
		else exit('The specified cache directory does not exists!');
	}

	/**
	* Set the left delimiter to use in the templates files to mark a template variable.
	*
	* @param string $delim  The left delimiter to use in the templates files to mark a template variable.
	* @return void
	*/
	public function SetLeftDelimiter( $delim )
	{
		if ( ! empty($delim))
			$this->left_delimiter = $delim;
	}

	/**
	* Set the right delimiter to use in the templates files to mark a template variable.
	*
	* @param string $delim  The right delimiter to use in the templates files to mark a template variable.
	* @return void
	*/
	public function SetRightDelimiter( $delim )
	{
		if ( ! empty($delim))
			$this->right_delimiter = $delim;
	}

	/**
	* 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 void
	*/
	public function SetVar( $name, $value )
	{
		$this->vars[$name] = $value;
	}

	/**
	* 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 )
	{
		if (isset($this->vars[$name]) and !empty($this->vars[$name]))
			return $this->vars[$name];
		else return '';
	}

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

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

	/**
	* Abstract. This method should be implemented by the concrete class.
	*
	* @param string $template  The name of the template file to load.
	* @param integer $expires  The length of time, in hours, a file should be cached.
	* @return string  The template's html content.
	*/
	public function GetTemplate( $template, $expires=NULL )
	{
		// caching enabled
		if ( !is_null($expires) and is_int($expires) )
		{
			$_expires = ($expires *60*60) + time();

			if ($this->IsCached($template))
			{
				if ($this->HasCacheExpired($template))
				{
					// cache the template again
					$content = $this->Parse($template);
					$this->CacheTemplate($template,$content,$_expires);
					return $content;
				}
				// get cached template
				else return $this->GetCachedFile($template);
			}
			else {
				// cache template
				$content = $this->Parse($template);
				$this->CacheTemplate($template,$content,$_expires);
				return $content;
			}
		}
		// no cache
		else return $this->Parse($template);
	}

	/**
	* Outputs the template's html content.
	*
	* @param string $template  The name of the template file to load.
	* @return string  The template file's content.
	*/
	public function Display( $template )
	{
		echo $this->GetTemplate($template);
	}



# CACHING METHODS
#======================

	/**
	* Delete all templates from the cache directory.
	*
	* @return void
	*/
	public function EmptyCacheDirectory()
	{
		$files = $this->GetCachedFiles();
		if (count($files) > 0) {
			foreach ($files as $file)
				@unlink($this->cache_dir.DIRECTORY_SEPARATOR.$file);
		}
	}

	/**
	* Delete a cached template.
	*
	* @param string $fileNames  The name(s) of the file(s) to delete.
	* @return void
	*/
	public function DeleteCached(/*$fileName, $fileName,...*/)
	{
		$files = func_get_args();
		if (count($files) > 1) {
			foreach ($files as $file) {
				$_file = $this->cache_dir.DIRECTORY_SEPARATOR.$this->SetCacheFileName($file);
				if (@file_exists($_file)) @unlink($_file);
			}
		}
	}

	/**
	* Check to see if the specified file exists in the cache directory.
	*
	* @param string $fileName  The name of the file to check for existance.
	* @return boolean
	*/
	public function IsCached( $fileName )
	{
		if (empty($fileName)) return FALSE;
		
		$file = $this->cache_dir.DIRECTORY_SEPARATOR.$this->SetCacheFileName($fileName);
		return (file_exists($file) ? TRUE : FALSE);
	}

	/**
	* Get the specified cached file's expire time.
	*
	* @param string $fileName  The name of the file.
	* @return string
	*/
	public function GetCacheExpireTime( $fileName )
	{
		$content = '';
		$lines = file($this->cache_dir.DIRECTORY_SEPARATOR.$this->SetCacheFileName($fileName));
		$expire_date = trim($lines[0]);
		$expire_date = substr($expire_date,1,-1);
		return $expire_date;
	}



# PROTECTED METHODS
#======================

	/**
	* Replaces the variables from the specified template file.
	*
	* @access protected
	* @param string $template  The name of the template file to load.
	* @return string  The template file's content.
	*/
	protected function Parse( $template )
	{
		ob_start();
			@include_once $this->tpl_dir.'/'.$template;
			$content = ob_get_contents();
		ob_end_clean();

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

	/**
	* Cache a template.
	*
	* @access protected
	* @param string $fileName  The name of the template to cache.
	* @param string $fileContent  The html/text content of the template file.
	* @param integer $expires In hours, the length of time the cached template should be kept in the cache folder.
	* @return void
	*/
	protected function CacheTemplate( $fileName, $fileContent, $expires )
	{
		// Create a new cache
		$name = $this->SetCacheFileName($fileName);
		$_expires = '['.$expires.']'."\n";
		$html_content = htmlentities($fileContent, ENT_QUOTES, 'UTF-8');

		$h = fopen($this->cache_dir.DIRECTORY_SEPARATOR.$name,'w');
		fwrite($h,$_expires.$html_content,strlen($_expires.$html_content));
		fclose($h);
	}

	/**
	* Check to see whether or not a specified cached file has expired.
	*
	* @access protected
	* @param string $fileName  The name of the file.
	* @return boolean
	*/
	protected function HasCacheExpired( $fileName )
	{
		$file_expire_date = (int) $this->GetCacheExpireTime($fileName);
		return ($file_expire_date >= time()) ? FALSE : TRUE;
	}

	/**
	* Set the name for the file to be cached.
	*
	* @access protected
	* @param string $file  The name of the template.
	* @return string
	*/
	protected function SetCacheFileName( $file )
	{
		return md5($file);
	}

	/**
	* Get all files from the cache directory.
	* @access protected
	* @return array
	*/
	protected function GetCachedFiles()
	{
		$fileList = array();
		$fileCount = 0;

		if ($dir = @opendir($this->cache_dir))
		{
			while ($file = @readdir($dir))
			{
				array_push($fileList, $file);
				$fileCount++;
			}
			@closedir($dir);
		}
		return $fileList;
	}

	/**
	* Get the content of a cached file
	*
	* @access protected
	* @param string $file  The name of the template.
	* @return string
	*/
	protected function GetCachedFile( $file )
	{
		$content = '';
		$lines = file($this->cache_dir.DIRECTORY_SEPARATOR.$this->SetCacheFileName($file));
		$all_lines = count($lines);
		for ($i=1; $i < $all_lines; $i++) // << except the first line that holds the file's expire time
		{
			$content .= html_entity_decode($lines[$i],ENT_QUOTES,'UTF-8');
		}
		return $content;
	}

}
// >> END
?>
Return current item: AbsTemplate