PHP
  Home arrow PHP arrow Page 4 - Simple PHP Templates With PatTemplate
IBM developerWorks
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  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
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? 
PHP

Simple PHP Templates With PatTemplate
By: Havard Lindset
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 169
    2002-06-26

    Table of Contents:
  • Simple PHP Templates With PatTemplate
  • An Overview Of PatTemplate
  • How to Process Template In PHP
  • Different Template Types
  • Template Visibility
  • 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
     
    Iron Speed
     
    ADVERTISEMENT

    At the virtual BlackBerry Technical Seminar 2008, you can ask your development questions directly of Research In Motion® (RIM) experts, and take advantage of learning opportunities designed uniquely for BlackBerry solution developers. Register Today!

    Simple PHP Templates With PatTemplate - Different Template Types


    (Page 4 of 6 )

    Another interesting thing that you can do with patTemplate is choose different types for your templates. This is done by adding an attribute called type to the <patTemplate:tmpl> tag.

    There are four different types to choose from. I'll explain what each of them does with extracts from the patTemplate documentation.

    Standard
    This is the default type. Nothing special about it.

    OddEven
    When you assign this type to a template, you can insert two subtemplates (<patTemplate:sub>). They will then alternate when the template is repeated.

    Condition
    With this type you can assign as many subtemplates as you want. You have to assign one variable that will be compared with all the conditions of the subtemplates. There are two special conditions: default (similar to the default in a switch statement in PHP), and empty, which will be displayed when there is no value in the condition variable.

    SimpleCondition
    By using the SimpleCondition you can assign a list of variables (using the requiredvars) that have to be set if the template has to be displayed. If one of these variables isn't set, the template won't be visible.

    Let's now go into depth for each one of these different types, exclusing the standard type, which we've already seen two examples of.

    OddEven Type
    Have you ever seen a table with two alterning colors? This can be done easily with patTemplate using the OddEven type - without any hardcoding!

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>Alternating colors</title>
    </head>
    <body>
    <table>
    <patTemplate:tmpl name="namelist" type="OddEven">
    <patTemplate:sub condition="odd">
    <tr>
    <td bgcolor="#EBEEF3">{NAME}</td>
    </tr>
    </patTemplate:sub>
    <patTemplate:sub condition="even">
    <tr>
    <td bgcolor="#FFF2CC">{NAME}</td>
    </tr>
    </patTemplate:sub>
    </patTemplate:tmpl>
    </table>
    </body>
    </html>
    </patTemplate:tmpl>


    What we have here isn't really that complicated. We have the normal <patTemplate:tmpl> tag, only now it contains the "OddEven" attribute. Inside the tag there are two subtemplates that can be alternated. You can probably see that they have two different conditions, "Odd" and "Even".

    Let's take a look at the PHP code:

    <?PHP
    // Include the patTemplate-file
    include("includes/patTemplate.php");

    // Initialize the patTemplate-class, and create an object
    $tmpl = new patTemplate();

    // Set which directory contains the template-files.
    $tmpl->setBasedir("templates");

    // Set which template-file to read.
    $tmpl->readTemplatesFromFile("example3.tmpl.html");

    // Create an array of names
    $names = array("NAME" => array("Paul", "John", "Roger", "Michael", "Sarah", "Michelle"));

    // Add the array
    $tmpl->addVars("namelist", $names);
    $tmpl->parseTemplate("namelist");

    // Parse and display template
    $tmpl->displayParsedTemplate("body");
    ?>


    The only thing that needs explaining here are the array and the addVars() function.

    The array $names is a multidimensional array. It's indexed by the variable name, which in this case is name. If I had more variables I could do it like this:

    array("NAME" => array("Paul", "John", "Roger", "Michael", "Sarah", "Michelle"), "OTHERVAR" => array("value1", "value2"));

    The addVars() function is simply a function that accepts an array with many variables at once.

    Condition type
    You can compare the condition type to a switch statement in PHP. It compares the conditionvar with different subtemplate-conditions. Here is an example:

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>Visiting conditions</title>
    </head>
    <body>
    <form action="example4.php">
    <select name="name">
    <option value="Bob" SELECTED>Bob</option>
    <option value="Janice">Janice</option>
    <option value="Morgan">Morgan</option>
    </select>
    <input type="submit">
    </form>
    <patTemplate:tmpl name="greeter" type="condition" conditionvar="NAME">
    <patTemplate:sub condition="Bob">
    Welcome chairman Bob. How are you today?
    </patTemplate:sub>
    <patTemplate:sub condition="Janice">
    Well hello, Janice the janitor. Mop the floors immediately!
    </patTemplate:sub>
    <patTemplate:sub condition="default">
    Well hello, guest.. What's your name?
    </patTemplate:sub>
    <patTemplate:sub condition="empty">
    I see nobody! Where are you?
    </patTemplate:sub>
    </patTemplate:tmpl>
    </body>
    </html>
    </patTemplate:tmpl>


    As you can see, this is actually pretty easy. Basically you're given a lot of options for the conditionvar through the subtemplates. If the condition matches the conditionvar, then the subtemplate is shown. Here is the PHP code for this example:

    <?PHP
    // Include the patTemplate-file
    include("includes/patTemplate.php");

    // Initialize the patTemplate-class, and create an object
    $tmpl = new patTemplate();

    // Set which directory contains the template-files.
    $tmpl->setBasedir("templates");

    // Set which template-file to read.
    $tmpl->readTemplatesFromFile("example4.tmpl.html");

    // Add the condition variable from what was selected in the dropdown box.
    $tmpl->addVar("greeter", "NAME", $_GET['name']);
    $tmpl->parseTemplate("greeter");

    // Parse and display template
    $tmpl->displayParsedTemplate("body");
    ?>


    By now you should probably understand what all of the above PHP code does. The addVar() line adds the conditionvar, and it gets it's value from the $_GET form array.

    SimpleCondition type
    When you're using the SimpleCondition type, you supply a list of variables (through the requiredvars attribute) that are needed for the template to be shown.

    Here is an example where the variable NAME is required.

    <patTemplate:tmpl name="body">
    <html>
    <head>
    <title>SimpleCondition</title>
    </head>
    <body>
    <patTemplate:tmpl name="varcheck" type="SimpleCondition" requiredvars="NAME">
    I am happy to announce that the variable "NAME" has been set with the value "{NAME}".
    </patTemplate:tmpl>
    </body>
    </html>
    </patTemplate:tmpl>


    If the variable NAME isn't set, then message inside the SimpleCondition template won't be visible. Here is the PHP code:

    <?PHP
    // Include the patTemplate-file
    include("includes/patTemplate.php");

    // Initialize the patTemplate-class, and create an object
    $tmpl = new patTemplate();

    // Set which directory contains the template-files.
    $tmpl->setBasedir("templates");

    // Set which template-file to read.
    $tmpl->readTemplatesFromFile("example5.tmpl.html");

    // Add the required variable
    $tmpl->addVar("varcheck", "NAME", "Rachel");
    $tmpl->parseTemplate("varcheck");

    // Parse and display template
    $tmpl->displayParsedTemplate("body");
    ?>


    You can try commenting out the addVar() line to see for yourself that the message won't be displayed.

    More PHP Articles
    More By Havard Lindset


     

    PHP ARTICLES

    - Making Usage Statistics in PHP
    - Installing PHP under Windows: Further Config...
    - File Version Management in PHP
    - Statistical View of Data in a Clustered Bar ...
    - Creating a Multi-File Upload Script in PHP
    - Executing Microsoft SQL Server Stored Proced...
    - Code 10x More Efficiently Using Data Access ...
    - A Few Tips for Speeding Up PHP Code
    - The Modular Web Page
    - Quick E-Commerce with PHP and PayPal
    - Regression Testing With JMeter
    - Building an Iterator with PHP
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP


    Iron Speed





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