Location: PHPKode > projects > PHP Half-Life Monitor > phlmon-1.0.2-beta/template.php
<?php

#    PHP Half-Life Monitor 1.0.2-beta
#
#    $Id: template.php,v 1.10 2004/04/02 21:06:13 sloede Exp $
#
#    This file is part of the PHP Half-Life Monitor program
#
#    (c) 2002-2004 by Michael Schlottke <hide@address.com> <http://phlmon.sloede.com>
#
#    PHP Half-Life Monitor (short: PHLMon ) is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PHLMon; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

class template
{
	var $templatesDirectory = null;
	var $session = null;
	var $template = null;
	var $assignedVars = array();
	
	function template( $template )
	{
		$this->templatesDirectory = 'templates/'.$template.'/';
	}
	
	function show( $templateFile )
	{
		$this->loadTemplate( $templateFile );
		$this->assignVariables();
		$this->manageSections();
		echo $this->template;
	}
	
	function get( $templateFile )
	{
		$this->loadTemplate( $templateFile );
		$this->assignVariables();
		$this->manageSections();
		return $this->template;
	}
	
	function loadTemplate( $templateFile )
	{
	    file_exists($this->templatesDirectory.$templateFile) or
	        die("ERROR - Template file is missing: <b>"
	            .$this->templatesDirectory.$templateFile."</b><br><br>Aborting...");
		$templateArray = file( $this->templatesDirectory.$templateFile );
		$this->template = implode( '', $templateArray );
	}
	
	function assignVariables()
	{
		$this->assign( 'session', $this->session );
		foreach( $this->assignedVars AS $key => $value ) {
			$this -> template = preg_replace( "/{\s*\\\$$key\s*}/", $value, $this->template );
		}
	}
	
	function manageSections()
	{
		while( preg_match( "/{\s*%(\w+)+\s*}[\w\W]+?{\s*%\s*}/", $this->template, $matches ) ) {
			if ( $this->assignedVars[$matches[1]] ) {
				$this -> template = preg_replace( "/{\s*%".$matches[1]."+\s*}([\w\W]+?){\s*%\s*}/", "$1", $this -> template );
			} else {
				$this -> template = preg_replace( "/{\s*%".$matches[1]."+\s*}([\w\W]+?){\s*%\s*}/", "", $this -> template );
			}
		}
	}
	
	function assign( $var, $value )
	{
		if ( is_array( $var ) ) {
			foreach( $var AS $key => $value ) {
				$this->assign( $key, $value );
			}
		} else {
			$this->assignedVars[$var] = $value;
		}
	}

	function destroy()
	{
		$this->assignedVars = array();
		$this->template = null;
	}
}

?>
Return current item: PHP Half-Life Monitor