Home arrow PHP arrow Page 8 - Template-Based Web Development With patTemplate (part 2)

Fortune Favours The Brave - PHP

Got the basics down? Well, here's the advanced course - thisarticle demonstrates some of patTemplate's more sophisticated features,including the ability to dynamically show or hide templates, inheritvariables, use loops and conditional branches, and create dynamic,template-based forms and error handlers.

  1. Template-Based Web Development With patTemplate (part 2)
  2. Scoping It Down
  3. Speaking In Tongues
  4. Looping The Loop
  5. Legal Eagles
  6. Hide And Seek
  7. Setting Things Right
  8. Fortune Favours The Brave
  9. Running On Empty
  10. Simple Simon
  11. Brain Dump
  12. A Well-Formed Example
  13. Crash Bang Boom
  14. Endgame
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 10
June 19, 2002

print this article


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: