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

Setting Things Right - 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


All the examples you've seen thus far have had their attributes set at design time. This is not very useful in the majority of the cases; most often, you'd prefer to alter attributes like the loop counter or the visibility at run time, on the basis of logic in your script.

Luckily, patTemplate knows this - and it allows you to accomplish this goal via the very cool setAttribute() method, which allows you to dynamically set template attributes on the fly. Consider the following example, which demonstrates:

<!-- index.tmpl --> <patTemplate:tmpl name="index"> <html> <head><basefont face="Arial"></head> <body> Hello, and welcome to my Web site! <!-- blah blah --> <patTemplate:link src="tip" /> </body> </html> </patTemplate:tmpl> <patTemplate:tmpl name="tip" visibility="hidden"> <p><hr> <font size="-1">New user tip: Use the Find box at the top right corner of your screen to quickly search this site.</font> </patTemplate:tmpl>
As you can see, there are two templates in the file above; the second one is initially hidden from view. However, I can turn it on in certain cases - such as, for example, when a user visits the site for the first time. Here's the script that takes care of this for me.

<?php // alter this to see how the script functions // when the variable is unset $newUser = true; // 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("index.tmpl"); // turn tips on if new user if ($newUser == true) { $template->setAttribute("tip", "visibility", "visible"); } // parse and display template $template->displayParsedTemplate("index"); ?>
Depending on the value of a particular variable, I can turn the second template on or off, via a call to setAttribute().

You can use the setAttribute() method to manipulate other template attributes as well - try it with the "loop" or "varscope" attributes to see how it works.

>>> 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: