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;$idoReplace);$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(); ?>