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;$idoReplaceArray);$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;$adoReplaceArray[$i]);$l++){ $loopedContent[$i][$a][$l] = $looped[$a]; } } } } for($m=0;$m=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;$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); } } } /* * 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(); ?>