Location: PHPKode > scripts > Separated Template > separated-template/sTemplate.class.php
<?php
/**
 * sTemplate class
 * 
 * New way to use templates.
 * 
 * @author Sebastian Potasiak
 * @version 1.2 BETA
 * @update 27-06-2009
 * @license Creative Commons 2.5 BY-NC-SA (http://creativecommons.org/licenses/by-nc-sa/2.5/)
*/
class sTemplate
{
	public $dir = './templates/'; // Templates dir
	public $ext = '.html'; // Template ext.
	public $atr = 'class';
	
	/**
	 * show_error()
	 * 
	 * Showing an error
	 * 
	 * @param message [string] - Error message content
	 * 
	 * @access private
	 * @return string
	*/
	private function show_error($message)
	{
		echo '<b>Error:</b> ' . $message . '<br /><br />';
		return $message;
	}
	
	/**
	 * load()
	 * 
	 * Loading template
	 * 
	 * @param template [string] - Template filename
	 * @param values [array] d:0 - Values to replace
	 * @param show [boolean] d:false - Show template?
	 * 
	 * @access public
	 * @return string
	*/
	public function load($template, $values = 0, $show = false)
	{
		$path = $this->dir . $template . $this->ext;
		
		if (file_exists($path) && is_readable($path))
		{
			$file = file($path);
			$content = implode('', $file);
			
			if ($values != 0 && is_array($values))
			{
				foreach ($values as $key => $value)
				{				
					$content = preg_replace(
						'#\<(.*?) class=(\'|\"){1}' . $key . '(\s?)(.*?)\>(.*?)\</(.*?)\>#i',
						'<\\1 class=\\2' . $key . '\\3\\4>\\5' . $value . '</\\6>',
						$content 
					);
				}
			}
			
			if ($show == true) echo $content;
			return $content;		
		}	
		else
		{
			$this->show_error('File does not exists or is not readable.');
			return '';		
		}			
	}	
}
?>
Return current item: Separated Template