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

/*========================================================*\
||########################################################||
||#                                                      #||
||#     WB News v2.0.0                                   #||
||# ---------------------------------------------------- #||
||#     Copyright (c) 2004-2007                          #||
||#     Created: 22nd Dec 2006                           #||
||#     Filename: TemplateEngine.php                     #||
||#                                                      #||
||########################################################||
/*========================================================*/

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

class TemplateEngine
{

	var $filename = NULL;
	var $_params = array();

	var $ouput = TRUE;

	/**
	 *
	 *	@param String $filename this is optional
	 *	@param Bool $output this is optional if true we echo the output otherwise return it
	 */
	function TemplateEngine( $filename = NULL, $output = TRUE )
	{
		if ( NULL != $filename && file_exists($filename) )
		{
			$this->filename = $filename;
		}

		$this->output = $output;
	}

	/**
	 *
	 *	@param String $filename
	 *	@param boolean $reset_params if TRUE we empty _params
	 *	@return bool
	 */
	function SetFilename( $filename, $reset_params = FALSE )
	{
		if ( file_exists($filename) )
		{
			$this->filename = $filename;
			return TRUE;
		}

		if ( $reset_params )
		{
			$this->_params = array();
		}

		return FALSE;
	}

	/**
	 *
	 *	@param Bool $output preferably boolean type however any proper output will make
	 *						sure its TRUE otherwise false
	 *	@return bool
	 */
	function SetOutput( $output )
	{
		$this->output = ( $output ) ? TRUE :  FALSE ;
		return TRUE;
	}

	/**
	 *	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];
	}

	/**
	 *	TODO actually include HTML Template Files if not NULL
	 *
	 *	Returns a pager variable to the template
	 *
	 *	@param int $count
	 *	@param int $offset
	 *	@param int $limit
	 *	@param string $tmpl
	 *	
	 *	@return void
	 */
	function Pagination( $count, $offset, $limit, $file = NULL )
	{
		if ( NULL == $file )
		{
			$file = USE_AJAX ? LIBDIR . "/TemplateEngine/prev-next.ixml" : LIBDIR . "/TemplateEngine/prev-next.ihtml";
		}

		$pager =& $this->AddParam( "pager", NULL );
		$tmpl = new TemplateEngine( $file, FALSE );
		$prev_link =& $tmpl->AddParam( "prev_link", NULL );
		$next_link =& $tmpl->AddParam( "next_link", NULL );
		$first_link =& $tmpl->AddParam( "first_link", NULL );
		$last_link =& $tmpl->AddParam( "last_link", NULL );
		
		if ( $offset != 0 )
		{
			if ( ($offset-$limit) > 0 )
			{
				$prev_link = make_url_html( array("offset" => ($offset-$limit)) );
			}
			else if ( ($offset-$limit) == 0 )
			{
				$prev_link = make_url_html( array("offset" => FALSE ) );
			}
			
			$first_link = make_url_html( array("offset" => FALSE ) );
		}

		if ( ($offset+$limit) <= $count && ($count != ($offset+$limit)) )
		{
			$next_link = make_url_html( array("offset" => ($offset+$limit)) );
			$last_link = make_url_html( array("offset" => (floor(($count/$limit)) * $limit) ) );
		}

		if ( NULL == $prev_link && NULL == $next_link )
		{
			$pager = FALSE;
			return;
		}

		$pager = $tmpl->GetHTML();
		return;
	}

	/**
	 *	Returns HTML as either output or back to calling script
	 *
	 *	@return mixed 
	 */
	function GetHTML()
	{
		global $locale;
		
		if ( !$this->filename )
		{
			return NULL;
		}
	
		/**
		 *	this is nifty lets us go from $this->_param["name"] = "Paul"; to $name = "Paul";
		 */
		extract( $this->_params );

		/** load theme_info.php **/
		$path = BuildPath( "admin/theme_info.php" );
		if ( $path )
		{
			include( $path );
		}
		
		ob_start();
		include( $this->filename );
		$contents = ob_get_contents();
		ob_end_clean();

		if ( $this->output )
		{
			echo $contents;
			return;
		}

		return $contents;
	}

}

return;

?>
Return current item: WB News