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