Avoid duplication of page elements
“This is really good”, you may be thinking, “My website is mainly made up of a lot of static pages. Now I can remove the common parts of them from all the pages, It's too troublesome to update these common parts. In the future, I can use templates to create a unified page layout that is easy to maintain. "But it is not that simple, and the "large number of static pages" reveals the problem.
Consider the example above. This example actually only has one example.php page. The reason why it can generate all pages of the entire website is that it uses the query string in the URL to dynamically construct pages from information sources such as databases.
Most of us run websites that don’t necessarily have database support. Most of our website is composed of static pages, and then PHP is used to add some dynamic functions here and there, such as search engines, feedback forms, etc. So, how to apply templates on this kind of website?
The simplest method is to copy a PHP file for each page, and then set the variables representing the content in the PHP code to the appropriate page content in each page. For example, suppose there are three pages, namely home, about, and product. We can use three files to generate them respectively. The contents of these three files are similar to:
// home.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm' ,
'leftnav' => 'leftnav.htm' ) );
$content = "
Welcome
Hope you like this website
";