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

Music To Your Ears - 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
While on the subject, it's important to note that it isn't even necessary for all your templates to be stored in one physical file - patTemplate allows you the flexibility to store your templates in as many physical containers as you like, and to organize them in the manner that best suits your project. The template engine comes with a fairly well-developed API to read and combine the templates in the different files together - as the following example demonstrates:


<!-- common page elements --> <!-- stored in file common.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 footer --> <patTemplate:tmpl name="footer"> <p>&nbsp;<p align="right"> <font size="-2">{COPYRIGHT}</font> </body> </html> </patTemplate:tmpl> <!-- page body --> <!-- stored in file music.tmpl --> <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> <!-- main page --> <!-- stored in file main.tmpl --> <patTemplate:tmpl name="main"> <patTemplate:link src="header" /> <patTemplate:link src="body" /> <patTemplate:link src="footer" /> </patTemplate:tmpl>
Here's the script which puts them all together:

<?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("music.tmpl"); $template->readTemplatesFromFile("common.tmpl"); $template->readTemplatesFromFile("main.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"); ?>
And here's the output:



Simple, huh?

 
 
>>> 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 11 - Follow our Sitemap

Dev Shed Tutorial Topics: