PHP
  Home arrow PHP arrow Page 3 - Completing an Extensible Website Engine with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Completing an Extensible Website Engine with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 12
    2006-10-31


    Table of Contents:
  • Completing an Extensible Website Engine with PHP 5
  • Pulling web page contents from a database table
  • Injecting web page contents into the template file
  • The website engine in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Completing an Extensible Website Engine with PHP 5 - Injecting web page contents into the template file
    ( Page 3 of 4 )

    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.='&nbsp;'.$col.'&nbsp;';
                        }
                        $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!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek