MySQL
  Home arrow MySQL arrow Page 2 - Writing Your Own Template Caching Class In...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
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? 
MYSQL

Writing Your Own Template Caching Class In PHP
By: Havard Lindset
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2002-11-19

    Table of Contents:
  • Writing Your Own Template Caching Class In PHP
  • How Is It Supposed To Work?
  • The Code Explained (contd.)
  • The Code Explained (contd.)
  • Real-World Examples
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Writing Your Own Template Caching Class In PHP - How Is It Supposed To Work?


    (Page 2 of 6 )

    Before we start programming, I'll take you through a quick and easy example on how the templates and the template engine will work. Here's a simple template:

    <html>
    <head>
    <title>Simple template example</title>
    </head>

    <body>

    My first name is <?=$firstname?>, and my last name is <?=$lastname?>

    </body>
    </html>


    Here's how we'll add the variables to it using the template class we're about to write:

    <?php

    require_once('include/Template.php');

    $tpl = new Template();

    $tpl->addVar('firstname', 'John');
    $tpl->addVar('lastname', 'Doe');

    $tpl->display('templates/example.tpl.html');

    ?>


    Pretty easy, hey? The output of this example would be: "My first name is John, and my last name is Doe". As I explained above, the template is actually using normal PHP syntax for displaying the variables. This means that you don't have to learn another language for making templates.

    I'll be using short tags throughout this tutorial. Short tags can be turned off in your php.ini file, so using them isn't always the best alternative. If you want maximum portability for your templates, you should use long tags:

    <?php ?> instead of <? ?>

    <?php echo $variable ?> instead of <?=$variable?>

    Long tags cannot be turned off, so there won't be any problems with portability if you're using them. All right, it's time to get down to business and start programming.

    Writing the Class
    Let's start by declaring the class:

    <?php

    class Template {

    var $_variables = array();
    var $_caching = false;
    var $_cacheDir = '/cache';
    var $_cacheLifetime = 120;
    }
    ?>


    We've now declared the class and four properties:
    • The $_variables property will be used for storing the variables that are added to templates.
    • The $_caching property is used for deciding whether or not templates should be saved to a cache.
    • The $_cacheDir property is used for telling the application where it's supposed to save the cache.
    • The $_cacheLifetime property is used for deciding how long the cached files are supposed to live.
    We can now continue on to writing the methods. We'll start with addVar():

    function addVar($name, $value)
    {
    $variables = &$this->_variables;
    $variables[$name] = $value;
    }


    This method is pretty straightforward. We make a reference to $this->_variables by placing the & before it, and then add a new index to the array. If you haven't heard about references before, you can read about them in the php manual. Now that we have the addVar method, we can make the methods we'll use for displaying the templates:

    function display($file, $id = false)
    {
    echo $this->fetch($file, $id);
    }


    This method probably doesn't make any sense to you right now, because we haven't declared the fetch method yet. But we'll just stop here for a bit, and I'll explain what the different variables are. The $file variable is the path to the template file you want to parse, and the $id variable is used for caching. You don't have to use the $id variable, but it's here for caching dynamic pages. Here's an example:

    If you have a news-page that shows different news-items based on an article-id, you can use that article-id here to make a new cache file for each news-item. If you don't use the $id variable on pages like the one I described above, the cache will only save one file, which means that no matter what article-id you supply, you'll only get the one cached value. If you didn't understand this right now, don't worry. I have an example in this article where the $id variable is used.

    function fetch($file, $id = false)
    {
    if ($this->_caching == true && $this->isCached($file, $id)) {
    $output = $this->_getCache($file, $id);
    } else {
    $output = $this->_getOutput($file);

    if ($this->_caching == true) {
    $this->_addCache($output, $file, $id);
    }
    }

    return isset($output) ? $output : false;
    }

    More MySQL Articles
    More By Havard Lindset


     

    MYSQL ARTICLES

    - MySQL and BLOBs
    - Two Lessons in ASP and MySQL
    - Lord Of The Strings Part 2
    - Lord Of The Strings Part 1
    - Importing Data into MySQL with Navicat
    - Building a Sustainable Web Site
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - PhpED 3.2 – More Features Than You Can Poke ...
    - Creating An Online Photo Album with PHP and ...
    - Creating An Online Photo Album with PHP and ...
    - Security and Sessions in PHP
    - Setup Your Personal Reminder System Using PHP
    - Create a IP-Country Database Using PERL and ...
    - Developing a Dynamic Document Search in PHP ...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT