PHPBuilder - Template Framework For Static Sites Page 4

RSS Twitter

Template Framework For Static Sites - Page 4

by: Matthew Kendall
November 21, 2000

Avoiding Duplication of Repeated Elements

"That's great", you might think. "I have a site full of largely static pages. Now I can remove all the common elements from all my pages, which were a real pain to keep updated, and using templates I can generate an easily maintainable common layout". Not so fast. That phrase "largely static" is a problem.
Consider the example above. There is in fact only one page, example.php. The reason it can generate a whole site is that it uses information in the URL to construct the page dynamically, from information in a database.
Many of us do not run completely database-backed sites. We have sites composed of many pages of static content, and we use PHP here and there to add some functionality -- a search engine perhaps, or a feedback form. How to apply templates to such a site?
The simple solution is to duplicate the PHP file above for each page, and to set the content variable to the right page content in each one. For example, if I had three pages; home, about and products, I might generate them with three files that each looked like this:


// home.php
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );

$content "<p>Welcome to my web site.</p>
              <img src=\"demo.jpg\">
              <p>I hope you like it.</p>"


There are three obvious problems with this: we have to duplicate this complex template-related PHP code on every page, which is just as un-maintainable as duplicating the common HTML elements; the file now has HTML mixed in with the PHP again; and generating the content variable is going to be tedious because we are going to have to escape special characters.
The key to solving the problem is to address the mixing of PHP and HTML, not by removing the HTML from this file, but by removing most of the PHP.

« Previous Page
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Matthew Kendall



(Maximum characters: 1200). You have characters left.