PHP
  Home arrow PHP arrow Page 8 - Template-Based Web Development With pa...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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

Template-Based Web Development With patTemplate (part 2)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2002-06-19

    Table of Contents:
  • Template-Based Web Development With patTemplate (part 2)
  • Scoping It Down
  • Speaking In Tongues
  • Looping The Loop
  • Legal Eagles
  • Hide And Seek
  • Setting Things Right
  • Fortune Favours The Brave
  • Running On Empty
  • Simple Simon
  • Brain Dump
  • A Well-Formed Example
  • Crash Bang Boom
  • Endgame

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Template-Based Web Development With patTemplate (part 2) - Fortune Favours The Brave


    (Page 8 of 14 )

    Thus far, I've been working with what patTemplate refers to as "standard" templates. However, the patTemplate class also comes with some decidedly non-standard - and rather cool - alternatives...and one of the neatest ones has to be its support for conditional templates.

    Conditional templates function much like a series of "if-else" conditional statements - they allow you to display different output depending on how a particular, user-defined condition is evaluated. A conditional template typically contains a number of sub-templates, each keyed against a particular variable; depending on the value of that variable, the appropriate sub-template is extracted and used.

    In order to better understand this, consider the following simple example:


    <patTemplate:tmpl name="fortune" type="condition" conditionvar="DAY"> <html> <head> <basefont face="Arial"> </head> <body> And today's fortune is: <br> <patTemplate:sub condition="Mon"> Never make anything simple and efficient when a way can be found to make it complex and wonderful. </patTemplate:sub> <patTemplate:sub condition="Tue"> Life is a game of bridge -- and you've just been finessed. </patTemplate:sub> <patTemplate:sub condition="Wed"> What sane person could live in this world and not be crazy? </patTemplate:sub> <patTemplate:sub condition="Thu"> Don't get mad, get interest. </patTemplate:sub> <patTemplate:sub condition="Fri"> Just go with the flow control, roll with the crunches, and, when you get a prompt, type like hell. </patTemplate:sub> </body> </html> </patTemplate:tmpl>
    A little analysis, and you'll see that this isn't as complicated as it looks. The outer "fortune" template has been defined as a conditional template by the addition of two attributes to the <patTemplate:tmpl> tag - the "type" attribute, which is set to the value "condition", and the "conditionvar" attribute, which is set to the name of the decision variable to be used during the evaluation process.

    This conditional template is then broken up into individual sub-templates, enclosed within <patTemplate:sub>...</patTemplate:sub> tags, and each possessing a "condition" attribute. This condition attribute specifies the value of the decision variable that the template engine will use when deciding which sub-template to display.

    Here's the other half of the puzzle - the PHP script that actually sets a value for the decision variable so that the template engine can select an appropriate sub-template.

    <?php // include the class include("include/patTemplate.php"); // initialize an object of the class $template = new patTemplate(); // set template location $template->setBasedir("templates"); // add templates to the template engine $template->readTemplatesFromFile("fortune.tmpl"); $template->AddVar("fortune", "DAY", date("D", mktime())); // parse and display the template $template->displayParsedTemplate("fortune"); ?>
    In this case, the PHP script merely sets the value of the template variable {DAY} - you'll remember that this is the decision variable defined in the conditional template - to the current day of the week, and then displays the parsed template. Internally, the template engine will match the value of {DAY} to the options available in the various sub-templates, and pick the one that fits.

    As you can see, this is identical to a "switch" statement, or a series of "if-else" conditional statements - and it can come in fairly handy at times.

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


     

       

    PHP ARTICLES

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...




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