Location: PHPKode > projects > WB News > includes/lib/PageTemplateEngine.php
<?php

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 10th Apr 2007                           #||
||#     Filename: PageTemplateEngine.php                 #||
||#                                                      #||
||########################################################||
/*========================================================*/

/**
 *  @author $Author: pmcilwaine $
 *	@version $Id: PageTemplateEngine.php,v 1.1.2.3 2008/06/21 02:17:44 pmcilwaine Exp $
 */

class PageTemplateEngine
{

	var $filename = NULL;
	var $title = NULL;
	var $head = NULL;
	var $body = NULL;
	var $_params = array();
	
	/**
		the key is the div class and if TRUE we remove the entire div
		if FALSE we keep the div **/
	var $special_div_list = array(
		"big_form" => TRUE
		);
		
	// store the special divs in array so we can use in the main
	var $special_divs = array();

	/**
	 *
	 *	@param string $filename
	 */
	function PageTemplateEngine( $filename )
	{
		if ( !file_exists($filename) )
		{
			echo "Template Engine could not load file $filename";
			return;
		}

		$this->filename = $filename;
	}
	
	/**
	 * void BindParam( string $param, object $obj );
	 * 
	 * @param string $param
	 * @param object $obj
	 * @return void
	 */
	function BindParam( $param, $obj )
	{
		$this->_params[ $param ] =& $obj->_params[ $param ];
	}

	function ParseContent( $content )
	{
		if ( NULL == $this->filename )
		{
			return;
		}
		
		/** match head & title tags **/
		$foundHead = preg_match( "/<head>(.+?)<\/head>/is", $content, $head );
		if ( $foundHead )
		{
			list( , $head ) = $head;
		}
		
		// get Title Tag
		$foundTitle = FALSE;
		if ( $foundHead )
		{
			$foundTitle = preg_match( "/<title>(.+?)<\/title>/i", $head, $title );
		}
		
		if ( $foundTitle )
		{
			list( , $title ) = $title;
			// now remove from match
			$head = preg_replace( "/<title>$title<\/title>/i", "", $head );
		}

		/** match body tag **/
 		$foundBody = preg_match( "/<body>(.+?)<\/body>/is", $content, $body );
		if ( $foundBody )
		{
			list( , $body ) = $body ;
		}

		$this->head = $foundHead ? ltrim($head) : NULL;
		$this->title = $foundTitle ? trim($title) : NULL;
		$this->body = $foundBody ? trim($body) : NULL;
		
		$this->ProcessSpecialDiv();
		
		return;
	}
	
	/**
	 * void SpecialDiv( void );
	 * 
	 * Stores all the special divs into an array and removes
	 * from the body
	 * 
	 */
	function ProcessSpecialDiv()
	{
	}

	/**
	 *	Creates a template variable. If a param already exists it will be
	 *	overwritten
	 *
	 *	@param String $name the name of the variable
	 *	@param String $value the value of the variable
	 *	@return mixed a reference to the new template variable
	 */
	function &AddParam( $name, $value )
	{
		$this->_params[$name] = $value;
		return $this->_params[$name];
	}

	/**
	 *	Returns HTML as either output or back to calling script
	 *
	 *	@return mixed 
	 */
	function ShowPage()
	{
		global $config;
		
		if ( NULL == $this->filename )
		{
			return;
		}
		
		extract( $this->_params );
		
		$title = $this->title;
		$head = $this->head;
		$body = $this->body;

		if ( isset( $_SESSION["msg"] ) )
		{
			$msg = $_SESSION["msg"];
			unset( $_SESSION["msg"] );
		}
	
		ob_start();
		include( $this->filename );
		$contents = ob_get_contents();
		ob_end_clean();

		echo $contents;
		return;
	}

}

?>
Return current item: WB News