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

Slice And Dice - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
It's also possible to split a single page into smaller, modular templates, and link these templates together to create new and interesting shapes and patters. Take a look at how this might be accomplished:


<!-- main page --> <patTemplate:tmpl name="main"> <patTemplate:link src="header" /> <patTemplate:link src="body" /> <patTemplate:link src="footer" /> </patTemplate:tmpl> <!-- page header --> <patTemplate:tmpl name="header"> <html> <head> <basefont face="Arial"> </head> <body bgcolor="navy" text="white" link="white" vlink="white" alink="white"> </patTemplate:tmpl> <!-- page body --> <patTemplate:tmpl name="body"> <center> Feelin' blue? How about a little <a href="http://www.melonfire.com/community/columns/boombox/">music</a>? </center> </patTemplate:tmpl> <!-- page footer --> <patTemplate:tmpl name="footer"> <p>&nbsp;<p align="right"> <font size="-2">{COPYRIGHT}</font> </body> </html> </patTemplate:tmpl>
In this case, I have three different templates, one each for the page header, body and footer. I also have a fourth template, this one merely containing links to the remaining three templates. When patTemplate parses this container template, it will find and follow the links to the other templates, parse them and display them.

Here's the script which does all the work:

<?php // include the class include("include/patTemplate.php"); // initialize an object of the class $template = new patTemplate(); // set template location $template->setBasedir("templates"); // set name of template file $template->readTemplatesFromFile("music.tmpl"); // assign values to template variables $template->AddVar("footer", "COPYRIGHT", "This material copyright Melonfire, " . date("Y", mktime())); // parse and display the template $template->DisplayParsedTemplate("main"); ?>
As you can see, it's almost identical to the one on the previous page - which only serves to demonstrate how transparent and simple the process of linking templates together is.

Here's what the output looks like:



If you have a Web page which consists of many individual pieces, you'll find this ability to create independent, modular templates immensely valuable - and once you start using it, you'll wonder how you ever worked without it!

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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: