PHP
  Home arrow PHP arrow Page 3 - Building a Site Engine with PHP, Part 4
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? 
PHP

Building a Site Engine with PHP, Part 4
By: James Murray
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 24
    2004-07-12


    Table of Contents:
  • Building a Site Engine with PHP, Part 4
  • You’ve been Tagged
  • Give it a Little Class:
  • The Final Words

  • 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


    Building a Site Engine with PHP, Part 4 - Give it a Little Class:
    ( Page 3 of 4 )

    Now we need to talk about the template plug-in and how it works. This is basically the only part of the entire engine that makes it only compatible with PHP5 because we use SimpleXML to parse our XML file. SimpleXML works a lot like your average PHP class. You simply set simplexml_load_file() to the value of a variable and call a file name with it to load the XML document into the script, then you use the -> operator to call each element of the XML file. Here’s a little thing you can do to test out simpleXML. Take the XML example I showed you earlier and save it as template.xml, then save the following script to the same directory as test.php.

    <?
    //load the template.xml file.
    $template = simpleXML_load_file(template.xml);
    //load the header element as a string
    $header = $template->header;
    //output the header element
    echo $header;
    ?>

    Go ahead and try that, then check the page source in your browser and you’ll see the exact html that’s in the header element of the template.xml file. Compare that to trying to make a parser do the same thing in PHP4 and you’ll understand why it’s called simpleXML. As you can see simplexml_load_file() basically loads the XML file into the $template variable and sets it up as if it’s a class and you call each element as if it were a class’s variable. Pretty simple isn’t it, hence the name simpleXML. Now that you know how simpleXML is going to help us load the template file, we’re going to talk about how to set up or plug-in so that it will easily load your template file into the engine. So, let’s go ahead and take a look at the class that handles the template system.

    First we start off by defining the class, and all the variables that we plan on using throughout the engine. $template_list is an array that holds a list of all the templates that we have, this is useful for listing them out on a page to get information on what templates you have, without having to look at the database. $cur_template will hold all the information on the template that we’re currently working with. $loaded is the place holder for the template file after we’ve loaded it with simpleXML. $elements is an array of all the elements in the XML file that aren’t columns. $columns is the array that will hold (gasp!) the column elements from our template file. $tags is a rather important variable. It holds all the tags that we use through out the engine. And last but not least, $page holds the final output of the entire engine that will be sent to the browser.

    class template{

    public $template_list;
    public $cur_template;
    public $loaded;
    public $elements;
    public $columns;
    public $tags;
    public $page;

    The template function has been named the same as the class so that it will run when we call the class. Basically it builds the list of templates that we can use with the particular site, from the database. Then we call a few functions so that those too load when the class is called.


    function template(){
    global $sql, $site;
    $templates = $sql->_query("SELECT t_ID, t_name, t_file, t_status, t_author, t_date FROM templates WHERE site_ID = '{$site->site['ID']}'");
    while($template=$sql->_fetch_object($templates)){
    $this->template_list['t_ID'][]=$template->t_ID;
    $this->template_list['t_name'][]=$template->t_name;
    $this->template_list['t_file'][]=$template->t_file;
    $this->template_list['t_status'][]=$template->t_status;
    $this->template_list['t_author'][]=$template->t_author;
    $this->template_list['t_date'][]=$template->t_date;
    }
    $this->set_template();
    $this->load_template();
    $this->tags['host']=$site->site['host'];
    }

    Set template loads the template that the user has set as the template they want to use as the $cur_template. If a user_ID isn’t found, it will load the template that has the status that’s set to “loaded” as the default template.


    function set_template(){
    global $sql,$user;
    if(isset($user->user['ID'])){
    $U_ID=$user->user['ID'];
    $user_template=$sql->_query("SELECT t_ID FROM template_users WHERE user_ID = '$U_ID'");
    while($user_temp=$sql->_fetch_object($user_template)){
    $tempate_key=array_keys($this->template_list['t_ID'],$user_temp->t_ID);
    }
    }else{
    $tempate_key=array_keys($this->template_list['t_status'],"loaded");
    }
    $this->cur_template['t_name']=$this->template_list['t_name'][$tempate_key[0]];
    $this->cur_template['t_file']=$this->template_list['t_file'][$tempate_key[0]];
    }

    Load_template actually gets all the information from the template’s XML file and breaks it down into each component. Then it sections them off into the correct array based on the name of the element. As you can see, if the element’s name isn’t “header”, “body”, “footer”, or “block”, it will get sent to the column's array because the function recognizes anything other than those four elements as a column, which releases you from the regular three column layout and lets you add as many columns as you’d like. You can have one column or fifty columns if you’d like. There's also a small amount of error handling put into the function so that if the template isn’t loaded, you’ll be informed and you can correct the problem, which is usually a misspelling of a templates name.


    function load_template(){
    global $site;
    if(is_array($this->cur_template)){
    if(file_exists("templates/{$site->site['host']}/{$this->cur_template['t_file']}/{$this->cur_template['t_name']}.xml")){
    $this->cur_template['template']="templates/{$site->site['host']}/{$this->cur_template['t_file']}/{$this->cur_template['t_name']}.xml";
    $this->loaded=simplexml_load_file($this->cur_template['template']);
    $elements = array_keys(get_object_vars($this->loaded));
    for($i=0;$i<count($elements);$i++){
    if($elements[$i]=="header"||$elements[$i]=="body"||$elements[$i]=="footer"||$elements[$i]=="block"){
    $this->elements[$elements[$i]]=$this->loaded->$elements[$i];
    }else{
    $this->columns[$elements[$i]]=$this->loaded->$elements[$i];
    }
    }
    }else{
    echo"ERROR Template <b>templates/{$site->site['host']}/{$this->cur_template['t_file']}/{$this->cur_template['t_name']}.xml</b> Not Found";
    }
    }else{
    echo"ERROR Template Data Not Specified";
    }
    }

    This is the function that we call from the blocks plug-in after we’ve loaded and sorted all the blocks. It reads the names of each column and matches them to the array of blocks that have been set by the parse_block() function in the blocks plug-in, then it sends all the information to the tags array as a single tag which replaces the <!content> tag in the body element.


    function build_cols($col_content,$bcolumns){
    global $blocks,$plugins;
    if(is_array($this->columns)){
    $columns_keys=array_keys($this->columns);
    foreach($columns_keys as $column){
    if(in_array($column,$bcolumns)){
    $this->tags['content'].=$this->replace($col_content,$this->loaded->$column);
    }
    }
    }
    }

    The parse_page function isn’t complicated at all, but it is still very important. It send the rest of the remaining elements to the replace function to get the tags replaced, then it puts them to the $page variable which I mentioned above that holds all the data that will be sent to the browser. And, just as an added amount of security that all our tags get replaced and to make sure any tags in the block’s loaded content get replaced, we run the entire $page variable through the replace function before the final output.


    function parse_page(){
    $this->page.=$this->replace($this->tags,$this->loaded->header);
    $this->page.=$this->replace($this->tags,$this->loaded->body);
    $this->page.=$this->replace($this->tags,$this->loaded->footer);
    return $this->replace($this->tags,$this->page);
    }
    ?>

    That’s about it for the template plug-in. Like all the other parts of the engine, it’s not actually all that complicated if you just take a second to look at it.



     
     
    >>> More PHP Articles          >>> More By James Murray
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT