Template-Based Web Development With patTemplate (part 1) - A Rose By Any Other Name...
(Page 8 of 8 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |