<?phpinclude "templateExceptions.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! (Note, this is only for the BODY pages, ie, header, mid, footer, not for * things like news pages, or where a template is needed in a template, see tempAdd.php for that. * * Daniel J. Enman * Saturday, June 24th 2006 */class template { /* * 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 = array(); public $replaceWith = array(); public $midfile; public $content; public $tempname; /* * Our constructor, this will only work in PHP5, for >PHP4 use __construct() */ function template($tempname){ $this->tempname = $tempname; /* * Check to see if some very important files exists */ if(!is_dir($tempname)){ die("There is no template with that name. Sucka."); }else if(!file_exists($tempname."/main.html")){ die("There is no file named ".$tempname."/main.html"); }else if(!file_exists($tempname."/main.css")){ die("There is no css file (".$tempname."/main.css)"); }else if(!file_exists($tempname."/header.html")){ die("There is no header file"); }else if(!file_exists($tempname."/footer.html")){ die("There is no footer file"); } } /* * Build the entire contents, and once we have it, we can do everything else we want to it later! */ function buildContents($midfile){ $content = 0; $this->midfile = $this->tempname."/".$midfile; if(!file_exists($this->midfile)){ die("The file you are trying to use as a template doesnt exists (error: 001)"); } // Catch the header file $tr = @fopen($this->tempname."/header.html", 'r'); if($tr){ $content = fread($tr, filesize($this->tempname."/header.html")); }else{ die("Could not read the file"); } // Release the file back into the wild! unset($tr); // Catch the middle file $tr = @fopen($this->midfile, 'r'); if($tr){ $content .= fread($tr, filesize($this->midfile)); } // NOW we're rollin', let's release this file too. unset($tr); // Catch the footer $tr = @fopen($this->tempname."/footer.html", 'r'); if($tr){ $content .= fread($tr, filesize($this->tempname."/footer.html")); } $this->content = $content; } /* * Get all the replacements in an array */ function replacements($replace, $with){ $i = count($this->doReplace); $j = count($this->replaceWith); $this->doReplace[$i] = $replace; $this->replaceWith[$j] = $with; } 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->doInclude(); return $this->content; }}$temp = new template("main");$temp->buildContents("urgeboxes.html");$temp->replacements('YAR!','title');$temp->replacements('f', 'hi');$temp->doReplacements();echo $temp->parseTemplate();?>