HomePHP Page 3 - Completing an Extensible Website Engine with PHP 5
Injecting web page contents into the template file - PHP
Are you one of those web developers searching for a comprehensive approach to constructing an expandable website engine with PHP 5? If your answer is a resounding yes, then this series might appeal to you. Welcome to the final part of the series “Building an extensible website engine with PHP 5.” In two tutorials, this series teaches you how to build a versatile website system which allows you to generate dynamic web documents by using a few simple PHP classes.
As I said in the previous section, to complete the pertinent website engine, it’s necessary to add an additional component to it, which will be responsible for parsing the respective template file. In this way, both the data and presentation layers will be interconnected to each other by the couple of MySQL processing classes shown previously, as well as by the template processor that I'm just about to create.
With reference to the template processor class, please take a look at its source code, which is listed below:
class TemplateProcessor { private $output='';// set default value for overall class output private $rowTag='p';// set default value for database row tag private $tags=array();// set default value for tags private $templateFile='templates/default_template.htm';// set default value for template file private $cacheFile='cache/default_cache.cache';// set default value for cache file private $expiry=1;// set default value for cache expiration public function __construct($tags=array()){ if(count($tags)<1){ throw new Exception('Invalid number of tags'); } if($this->isCacheValid()){ // read data from cache file $this->output=$this->readCache(); } else{ $this->tags=$tags; // read template file $this->output=file_get_contents($this->templateFile); // process template file $this->processTemplate($this->tags); // clean up empty tags $this->output=preg_replace("/{w}|}/",'',$this- >output); // write compressed data to cache file $this->writeCache(); } // send gzip encoding http header $this->sendEncodingHeader(); } // check cache validity private function isCacheValid(){ // determine if cache file is valid or not if(file_exists($this->cacheFile)&&filemtime($this- >cacheFile)>(time()-$this->expiry)){ return true; } return false; } // process template file private function processTemplate($tags){ foreach($tags as $tag=>$data){ // if data is array, traverse recursive array of tags if(is_array($data)){ $this->output=preg_replace("/\{\$tag/",'',$this- >output); $this->processTemplate($data); } // if data is a file, fetch processed file elseif(file_exists($data)){ $data=$this->processFile($data); } // if data is a MySQL result set, obtain a formatted list of database rows elseif(@get_resource_type($data)=='mysql result'){ $rows=''; while($row=mysql_fetch_row($data)){ $cols=''; foreach($row as $col){ $cols.=' '.$col.' '; } $rows.='<'.$this- >rowTag.'>'.$cols.'</'.$this->rowTag.'>'; } $data=$rows; } // if data contains the '[code]' elimiter, parse data as PHP code elseif(substr($data,0,6)=='[code]'){ $data=eval(substr($data,6)); } $this->output=str_replace('{'.$tag.'}',$data,$this- >output); } // clean up empty tags $this->output=preg_replace("/{\w+}/",'',$this->output); } // process input file private function processFile($file){ ob_start(); include($file); $contents=ob_get_contents(); ob_end_clean(); return $contents; } // write compressed data to cache file private function writeCache(){ if(!$fp=fopen($this->cacheFile,'w')){ throw new Exception('Error writing data to cache file'); } fwrite($fp,$this->getCompressedHTML()); fclose($fp); } // read compressed data from cache file private function readCache(){ if(!$cacheContents=file_get_contents($this->cacheFile)){ throw new Exception('Error reading data from cache file'); } return $cacheContents; } // return overall output public function getHTML(){ return $this->output; } // return compressed output private function getCompressedHTML(){ // check if browser supports gzip encoding if(strstr($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip')){ // start output buffer ob_start(); // echo page contents to output buffer echo $this->output; // compress (X)HTML output with gzip $this->output=gzencode(ob_get_contents(),9); // clean up output buffer ob_end_clean(); // return compressed (X)HTML content return $this->output; } return false; } // send gzip encoding http header private function sendEncodingHeader(){ header('Content-Encoding: gzip'); } }
As shown above, the “TemplateProcessor” class has some handy methods for replacing all the placeholders contained inside the “default_template.thm” file with actual web page data. Aside from the functionality that I just mentioned, this class is also capable of caching the parsed template during a given period of time, as well as sending parsed contents as compressed data via the “Gzip” compression algorithm.
However, it’s important to notice here that the “TemplateProcessor” class is itself the other critical part of the website engine. When used in conjunction with the previous MySQL processing classes, it is capable of generating true dynamic web pages even when the website’s whole structure is static (remember that each web document is created basically from the same “default_template.htm” file).
All right, now you’ve seen separately how each part of the website engine does its thing. On one hand you learned how to fetch the contents of each web page, while on the other hand you saw how these contents are injected straight into the previously defined template file. But how can these parts work together to implement a fully-functional website engine?
Well, in the next section, I’ll use all the classes that I defined before so you can see how this PHP 5-based website engine can generate web pages on the fly.
To learn how this will be achieved, all you have to do is click on the link shown below and keep reading. We’re almost finished!