Creating a template framework for static web sites with PHP (iii)
Source: Internet
Author: User
Avoid page element duplication
"It's really good," You might think, "My site is mostly composed of a lot of static pages." Now that I can remove their public parts from all the pages, it's too much trouble to update the public parts. In the future, I can create a unified page layout that is easy to maintain with templates. "But things are not so simple," a lot of static pages say the problem lies.
Please consider the above example. This example actually has only one example.php page, and it is able to generate all the pages of the entire site because it uses the query string in the URL to dynamically construct the page from information sources such as databases.
Most of us are running websites that don't necessarily have database support. Most of our site is composed of static pages, and then use PHP here, there are some dynamic features, such as search engines, feedback forms. So how do you apply a template on such a Web site?
The easiest way to do this is to copy a PHP file for each page, and then set the variables in the PHP code that represent the content in each page to the appropriate page content. For example, suppose you have three pages that are home, about (about), and products (product), and we can generate them separately with three files. The contents of these three files are as follows:
<p> hope you can enjoy this website </p> ";
$tpl->assign (' CONTENT ', $content);
$tpl->parse (' header ', ' header ');
$tpl->parse (' LeftNav ', ' leftnav ');
$TPL->parse (' main ', ' main ');
$tpl->fastprint (' MAIN ');
?>
Obviously, there are three problems with this approach: we have to replicate these complex, template-related PHP code for each page, which is as difficult to maintain as repeating common page elements; now the files are mixed with HTML and PHP code; Assigning values to content variables becomes very difficult, Because we have to deal with a lot of special characters.
The key to solving this problem is to separate the PHP code from the HTML content, although we can't remove all the HTML content from the file, but we can move out most of the PHP code.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service