PHP
  Home arrow PHP arrow Page 3 - Template-Based Web Development With patTemplate (part 1)
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

Template-Based Web Development With patTemplate (part 1)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2002-05-28


    Table of Contents:
  • Template-Based Web Development With patTemplate (part 1)
  • Hard Sell
  • Message In A Bottle
  • Slice And Dice
  • Music To Your Ears
  • Watching The Clock
  • A Bookworm In The Ointment
  • A Rose By Any Other Name...

  • 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


    Template-Based Web Development With patTemplate (part 1) - Message In A Bottle
    ( Page 3 of 8 )

    With the advertising out of the way, let's take a simple example to see how patTemplate works. Consider the following template:


    <patTemplate:tmpl name="message"> <html> <head> <basefont face="Arial"> </head> <body> <h2>{TITLE}</h2> {MESSAGE} </body> </html> </patTemplate:tmpl>
    The block above represents a single template, identified by the opening and closing <patTemplate:tmpl> tags, and by a unique name ("message", in this case).

    Within the opening and closing tags comes the actual template body; to all intents and purposes, this is a regular HTML document, except that the markup also contains some special patTemplate variables, written in uppercase and enclosed within curly braces. These special variables will be replaced with actual values once the template engine gets its mitts on it. Let's look at that next.{mospagebreak title=Anatomy Of A Template Engine} Next, it's time to initialize the template engine and have it populate the template created on the previous page with actual data. Here's how:


    <?php // include the class include("patTemplate.php"); // initialize an object of the class $template = new patTemplate(); // set template location $template->setBasedir("templates"); // set name of template file $template->readTemplatesFromFile("message.tmpl"); // set values for template variables $template->addVar("message", "TITLE", "Error"); $template->addVar("message", "MESSAGE", "Total system meltdown in progress"); // parse and display the template $template->DisplayParsedTemplate("message"); ?>
    This might seem a little complicated, so let me dissect it for you:

    1. The first step is, obviously, to include all the relevant files for the template engine. Since patTemplate is packaged as a single class, all I need to do is include that class file.

    // include the class include("patTemplate.php");
    Once that's done, I can safely create an object of the patTemplate class.

    // initialize an object of the class $template = new patTemplate();
    This object instance will serve as the primary control point for the template engine, allowing me to do all kinds of nifty things.

    2. Next, the object's setBaseDir() and readTemplatesFromFile() methods are used to point the engine in the direction of the templates to be read. The setBaseDir() method sets the default location for all template files; the readTemplatesFromFile() method specifies which template file to process.

    // set template location $template->setBasedir("templates"); // set name of template file $template->readTemplatesFromFile("message.tmpl");
    patTemplate allows multiple templates to be stored in the same physical file; the engine uses each template's name to uniquely identify it.

    3. Once all the templates in the specified file have been read, it's time to do a little variable interpolation. This is accomplished via the addVar() object method, which attaches values to template variables.

    // set values for template variables $template->addVar("message", "TITLE", "Error"); $template->addVar("message", "MESSAGE", "Total system meltdown in progress");
    In English, this translates to "find the template named 'message' and assign the value 'Error' to the placeholder {TITLE} within it ".

    You can run addVar() as many times as you like to perform variable interpolation.

    4. Once you're done with all the variable replacement, all that's left is to display the final product.

    // parse and display the template $template->DisplayParsedTemplate("message");
    The DisplayParsedTemplate() object method parses the specified template, replacing all variables within it with their specified values, and outputs it to the output device.

    In this specific example, the call to DisplayParsedTemplate() could also be broken down into two separate components, which combine to produce an equivalent result.

    // parse and display the template $template->parseTemplate("message"); echo $template->getParsedTemplate("message");
    In this version, the call to parseTemplate() merely parses the named template and replaces variables within it with appropriate values, while the call to getParsedTemplate() gets the contents of the parsed template and places it in a string variable.

    As you will see, this alternative version has its uses, especially when it comes to iteratively building or repeating a Web page. This is discussed in detail a little further along - for the moment, just feast your eyes on the result of all the work above:



    The nice thing about this approach? The page interface and page elements are separated from the program code that actually makes the page function - and can therefore be updated independently by Web designers and PHP developers.

     
     
    >>> More PHP Articles          >>> More By Team Melonfire, (c) Melonfire
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...





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