All pastes #80657 Raw Edit

enmand

public php v1 · immutable
·published 2006-07-06 17:49 UTC
rendered paste body
<?php/* *	URGE Template Engine, for URGE BaseStation (and Bytekill?) * *	This is nothing big, nothing like Smarty, but something a lot lighter, and more workable with what URGE needs. *	Not that many people will know the backend of things in URGE (unless it becomes part of the Bytekill code *	eventually). Until then, enjoy! * *																  Daniel J. Enman *																  Saturday, June 24th 2006 *//* *	This file is very similar the EntireContents.php file, with some minor changes */class SinglePage {	/*	 *	All te variable names should be self-explainatory, $doReplace is what the repleacements are going to be, 	 *	$midfile is the file(s) between the header and footer, $content is the entire file to be parsed, and 	 *	$tempname is the name of the template.	 */	public $doReplace;	public $replaceWith;	public $doReplaceArray;	public $replaceWithArray;	private $content;	public $tempname;	public $filename;	/*	 *	Our constructor, this will only work in PHP5, for >PHP4 use __construct()	 */	function SinglePage($tempname, $filename){		$this->tempname = $tempname;		$this->filename = $filename;		$this->file = $tempname."/".$filename;		/*		 *	Check to see if some very important files exists		 */		if(!is_dir($tempname)){			die("There is no template with that name.");		}else if(!file_exists($this->file)){			die("There is no file named ".$this->file);		}	}	/*	 *	Build the entire contents, and once we have it, we can do everything else we want to it later!	 */	function buildContents(){		$content = NULL;				if(!file_exists($this->file)){			die("The file you are trying to use as a template doesnt exists (error: 001)");		}		// Catch the file		$tr = @fopen($this->file, 'r');		if($tr){			$content = fread($tr, filesize($this->file));		}		// NOW we're rollin', let's release this file too.		unset($tr);		$this->content = $content;	}	/*	 * Get all the replacements in an array	 */	function replace($replace, $with){		if(!is_array($with)){			$i = count($this->doReplace);			$j = count($this->replaceWith);			$this->doReplace[$i] = $replace;			$this->replaceWith[$j] = $with;		}else{			$this->replaceArray($replace, $with);		}	}		function replaceArray($replace, $with){		$j = count($this->doReplaceArray);		$n = count($this->replaceWithArray);		if($n!=$j){			if($n>$j){				$ar = $n;			}else{				$ar = $j;			}		}else{			$ar = $j;		}		$withi = count($with);		for($i=0;$i<=count($withi);$i++){			$this->doReplaceArray[$ar][$i] = $replace;			$this->replaceWithArray[$ar][$i] = $with[$i];		}	}		function doReplacements(){		for($i=0;$i<count($this->doReplace);$i++){			while(preg_match_all('/%'.$this->doReplace[$i].'%/i', $this->content, $matches)>0){				$this->content = preg_replace('/%'.$this->doReplace[$i].'%/i', $this->replaceWith[$i], $this->content);			}		}	}		function doInclude(){		while(preg_match_all("/%include=((\.+)?(\/?\w+\/)?(\/)?(\w+\.php))/i", $this->content, $matches)>0){			$includes = preg_match("/%include=((\.+)?(\/?\w+\/)?(\/)?(\w+\.php))/i", $this->content , $matches);			$this->content = preg_replace("/%include=((\.+)?(\/?\w+\/)?(\/)?(\w+\.php))/i", '', $this->content);			include($matches[1]);		}	}		function parseTemplate(){		$this->doReplacements();		$this->doInclude();		return $this->content;	}}$temp = new SinglePage("main", "this.html");$temp->buildContents();$temp->replace('date', array(date(r), date('D M d Y')));$temp->replace('hi', 'Hello');echo $temp->parseTemplate();?>