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

A Rose By Any Other Name... - PHP

Most PHP-based Web sites are a mush of intermingled HTMLmarkupand PHP function calls, making them hard to decipher and maintain. Butthere *is* a simpler way - using templates to separate layout frombusinesslogic. This article shows you how.

  1. Template-Based Web Development With patTemplate (part 1)
  2. Hard Sell
  3. Message In A Bottle
  4. Slice And Dice
  5. Music To Your Ears
  6. Watching The Clock
  7. A Bookworm In The Ointment
  8. A Rose By Any Other Name...
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 9
May 28, 2002

print this article


So that's one look - but now how about changing it a little? Let's do away with the link boxes altogether, and have the links appear in neat rows at the bottom...

<!-- books.tmpl --> <!-- container page --> <patTemplate:tmpl name="main"> <html> <head> <basefont face="Arial"> </head> <body> <!-- header --> <img src="logo.gif" alt="Company logo"> <!-- content --> <patTemplate:link src="review" /> <p><hr> <patTemplate:link src="recommendations" /> <!-- footer --> <hr> <center><font size=-2>All content copyright and proprietary <a href="http://www.melonfire.com/">Melonfire</a>, 2002. All rights reserved.</font></center> </body> </html> </patTemplate:tmpl> <!-- review section --> <patTemplate:tmpl name="review"> <h2>{TITLE}</h2> <p> <img src="{POSTER}" width="100" height="100" alt="Book jacket" align="left">{CONTENT} </patTemplate:tmpl> <!-- reco section --> <patTemplate:tmpl name="recommendations"> <font size="-1">If you liked this title, you might also like:</font> <br> <table width="100%" cellspacing="3" cellpadding="3"> <tr> <patTemplate:link src="recommendations_list" /> </tr> </table> </patTemplate:tmpl> <!-- reco item section --> <patTemplate:tmpl name="recommendations_list"> <td><a href="story.php?id={ID}"><font size=-1>{ITEM}</font></a></td> </patTemplate:tmpl>
In this case, I've altered three of the templates to remove the tables and list constructs, so that I'm left with a very simple and elegant layout. Since all I'm doing is altering the layout, no changes are required to the PHP script itself; it should function as before.except that, this time, the output will look like this:

As you can see, patTemplate makes it possible to separate the user interface from the program logic, thereby allowing designers with little or no programming knowledge to alter Web pages quickly and easily. Further, with its ability to nest and repeat chunks of HTML code, it can speed up development time significantly, and also reduce the effort involved in maintaining and modifying a Web application.

And that's about it for the moment. In this article, you learned why using templates to build a Web application can save you time and effort during the post-release and maintenance phases of a Web project. You saw how the patTemplate system allows you to organize your user interface into separate, modular templates, and dynamically populate them via variable placeholders. You also saw how the patTemplate system allows you to link templates together, and to construct a complete Web page iteratively from atomic units. Finally, you put all those techniques to the test to create two different page layouts merely by altering the appropriate templates, with no changes required to the business logic of the application.

In the next (and concluding part) of this tutorial, I will be introducing you to some of patTemplate's more advanced features, including the ability to switch individual templates on and off, to create global variables, to automatically have your templates inherit variables from each other, and to create conditional templates and sub-templates. Make sure you come back for that!

Note: All examples in this article have been tested on Linux/i586 with Apache 1.3.20 and PHP 4.1.0. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

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