Template Framework For Static Sites - Page 3

The PHP code goes in a separate file. This is the file that is actually called
up by the page URL. The web server uses the PHP engine to parse this file and
returns the result to the browser. Typically the PHP code produces the content
for the page dynamically, perhaps by querying a database or performing a
calculation. The file might be as follows:

We are using the popular FastTemplate class here, but the idea is the same for
many of the available template classes. You instantiate the class, tell it
where to find the template files, and tell it which template file corresponds
to which page element. You generate the content for the page, perhaps by
querying a database, and assign the result to an identifier. Then you parse
the template files one by one and the template class performs the required
substitutions for you. Finally you output the resulting string to the browser.

The key benefit is that this file is entirely PHP. There is no HTML in it. The
PHP programmer can now concentrate on writing the code to generate the page
content without having to worry about generating HTML to format it nicely for
the final page.

You can build a whole site using this technique and just these files. If the
code that generates the page content does so on the basis of a query string in
the URL, you could build a magazine, for example, using a URL like
http://www.foo.com/example.php?article=099

On the face of it, it looks like we have a second benefit too. In the example,
the left-hand navigation bar is in its own file. We can change how it looks on
every page on our site by editing this one template file.