All pastes #97961 Raw Edit

Unnamed

public php v1 · immutable
#97961 ·published 2006-07-24 06:34 UTC
rendered paste body
<?phpinclude_once("main.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 *//* *	AUTHOR'S NOTE: Yes, I realize there are a lot of comments, and I have been told time and time again I leave "to many" *	comments in anything I write. (Although the people that told me that were amature fools ;-)). There is a reason for *	all the comments. It's so that any imbecile can come in and mess around in the file (although I wouldn't suggest it, *	PCRE can get pretty touchy), and it's so that the fool writing it can remember what he was doing at the time, and explain *	to himself what the hell has was doing at the time, so if you don't like the comments, that's to bad, and you'll have to *	suck if up and cry to your mommy. Enjoy. */class SinglePage extends templateEngine {	/*	 *	All the variable names should be self-explainatory, but then again, there is always the goof ball looking at the code``	 */	/*	 *	The 'variable' to be replaced in the template, for example, if $doReplace = 'moster', then {$monster} would be replaced	 *	in the template with the corresponding! $replaceWith	 *	 *	This variable has NO DEFAULT value, and a default value should never be set.	 */	public $doReplace;	/*	 *	The 'variable' that will replace the {$variable}, for example, if $doReplace = 'monster', and $replaceWith = 'Angus Gerard'	 *	then in the template file, {$monster} would be replaced with 'Angus Gerard'.	 *	 *	This variable has NO DEFAULT value, and a default value should never be set.	 */	public $replaceWith;	/*	 *	The same as $doReplace, except this handles arrays.	 *	 *	This variable has NO DEFAULT value, and a default value should never be set.	 */	public $doReplaceArray;	/*	 *	The same as $replaceWith, except this handles arrays.	 *	 *	This variable has NO DEFAULT value, and a default value should never be set.	 */	public $replaceWithArray;	/*	 *	The contents of the template file. During it's initial stages, it will include all the templateEngine code (ie. {$var}, {doLoop}, etc.)	 *	but during the course of the parse, it will all be fixed!	 *	 *	This variable has NO DEFAULT value, and a default value should never be set.	 */	private $content;	/*	 *	The name of the template (really the directory the template is in, ie. if $tempname = 'Monster", then the templateEngine would assume	 *	the template is in ./Monster/, however, if $tempname = '.', the template engine will assume the template is in the same directory as	 *	templateEngine.	 *	 *	This variable has A DEFAULT VALUE OF '.'	 */	public $tempname = '.';	/*	 *	The name of the template file, for example Angus.html. If $filename = "Gerard.html", then the templateEngine will look in $tempname/Gerard.html,	 *	so, if $tempname = 'Angus', and $filename = 'Gerard.html', templateEngine would look in Angus/Gerard.html for the contents for the $content	 *	variable!	 *	 *	This variable does not have a default value, and a default value should not be set	 */	public $filename;		/*	 *	For backwards compatibility, we use SinglePage (the class name) as our constructor. $tempname is NULL because a default value of '.'	 *	is set, meaning if $tempname isn't set in "new SinglePage()", then it will use the default value. This is our constructor, and is	 *	where all the basics happen. The filename is set here, and we check and make sure all the files and directories exist, since that	 *	is pretty important to our templateEngine.	 */	public function SinglePage($tempname=NULL, $filename){		// Check and see if we have to use the default value or not.		if($tempname!=NULL){			$this->tempname = $tempname;		}		$this->filename = $filename;		$this->file = $tempname."/".$filename;		/*		 *	Check to see if some very important files and directories exists. If there are any errors (like the directory of file not being there)		 *	throw a new exception (which wasn't include()'ed because of the __autoload() in main.php.		 */		if(!is_dir($tempname)){			throw new TemplateException("There is no template with that name.");		}else if(!file_exists($this->file)){			throw new TemplateException("There is no file named ".$this->file);		}	}	/*	 *	Now that we have made sure that all the files exist, it's time to use those files to build the contents of our	 *	template, so we can do things later like replacements, includes, add more templates, etc, etc! Hopefully the file isn't massive	 *	but just in case it is, we use unset($tr) to release the file back into the wild.	 */	public function buildContents(){		$content = NULL;				if(!file_exists($this->file)){			throw new TemplateException("The template does not exist");		}		// 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	 */	public 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);		}		$this->doReplacements();	}		protected function replaceArray($replace, $with){		/*		 *	If is an associate array, let's get in a form we can work with!		 */		$withi = count($with);		$a=0;		foreach($with as $i => $val){			$withTwo[$a] = $val;			$a++;		}		$with = $withTwo;		$j = count($this->doReplaceArray);		$n = count($this->replaceWithArray);		// This should never be true, but just incase		if($n!=$j){			if($n>$j){				$ar = $n;			}else{				$ar = $j;			}		}else{			$ar = $j;		}		for($i=0;$i<$withi;$i++){			$this->doReplaceArray[$ar][$i] = $replace;			$this->replaceWithArray[$ar][$i] = $with[$i];		}		$this->loopContent();	}		protected function loopContent(){		for($i=0;$i<count($this->doReplaceArray);$i++){			if(preg_match_all('/\{doLoop=\$'.$this->doReplaceArray[$i][0].'\}(.*?)\{endLoop=\$'.$this->doReplaceArray[$i][0].'\}/is', $this->content , $matches)>0){				echo 'try';				$looped = $matches[1];				$this->content = preg_replace('/\{doLoop=\$'.$this->doReplaceArray[$i][0].'\}(.*?)\{endLoop=\$'.$this->doReplaceArray[$i][0].'\}/is', "@\x00LOOP@\x00COUNT:".count($this->doReplaceArray[$i]), $this->content);				for($a=0;$a<count($looped);$a++){					for($l=0;$l<count($this->doReplaceArray[$i]);$l++){						$loopedContent[$i][$a][$l] = $looped[$a];					}				}			}		}		for($m=0;$m<count($loopedContent);$m++){			for($b=0;$b<count($loopedContent[$m]);$b++){				for($c=count($loopedContent[$m][$b]);$c>=0;$c--){					$loopedContentArray[$b] .= $loopedContent[$m][$b][$c];				}			}		}		print_r($loopedContentArray);	}		/*	 *	Do the replacements of the variables in the content, we everything works out fine!	 */	protected 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);			}		}	}	/*	 *	This is for including PHP or other type files, not for including other template (like header.html) files, use	 *	{addTemplate=} for that, although the two methods are similar.	 *		 *	You can use {include=} if you like, but {addTemplate=} is easier.	 	 	 // NOTE NOTE! HAVE TO DO PREG_SPLIT TO GET INCLUDE IN THE RIGHT PLACE!	 */	protected function doInclude(){		while(preg_match_all('/\{include=((\.+)?(\/?\w+\/)?(\/)?(\w+\..{0,5}))\}/i', $this->content, $matches)>0){			$includes = preg_match('/\{include=((\.+)?(\/?\w+\/)?(\/)?(\w+\..{0,5}))\}/i', $this->content , $matches);			$this->content = preg_replace('/\{include=((\.+)?(\/?\w+\/)?(\/)?(\w+\..{0,5}))\}/i', '', $this->content);			include($matches[1]);		}	}	/*	 * There are three types of avaliable comments, #, // and /* *\/ (Without the \), all of thes are valid in templateEngine. NOTE TO DEVELOPERS: The *? in the PCRE tells the . not to be greedy!	 */public function doComments(){	while(preg_match_all('/\/\*(.*?)\*\//is', $this->content, $matches)>0){		$this->content = preg_replace('/\/\*(.*?)(\*\/)/is', '', $this->content);	}	while(preg_match_all('/#(.*)/i', $this->content, $matches)>0){		$this->content = preg_replace('/#(.*)/i', '', $this->content);	}	while(preg_match_all('/\/\/(.*)/i', $this->content, $matches)>0){		$this->content = preg_replace('/\/\/(.*)/i', '', $this->content);	}}	public function parseTemplate(){		$this->doComments();		$this->doInclude();		return $this->content;	}}$temp = new SinglePage("main", "this.html");$temp->buildContents();/* *	Replacements work in the order they appear (isn't that obvious?) For example, hi would be replaced before lala */$temp->replace('hi', 'Hello');$temp->replace('date', array(date(r), date('D M d Y'), 'dance', 'why', 'won\'t', 'you', 'dance?'));$temp->replace('lala', array('a' => 'hi', 'there', 'how', 'are', 'you?'));$temp->replace('what', array('is', 'this', 'stuff?'));echo $temp->parseTemplate();?>