Creating a template framework for static web sites with PHP (ii)
Source: Internet
Author: User
The PHP code is saved in a separate file, which is the file that is actually called by the page URL. The Web server parses the file through the PHP engine and returns the results to the browser. Generally, PHP code always dynamically generates page content, such as querying a database or performing some sort of calculation. Here is an example:
The PHP code here is set $content to include the appropriate page content
$tpl->assign (' CONTENT ', $content);
$tpl->parse (' header ', ' header ');
$tpl->parse (' LeftNav ', ' leftnav ');
$TPL->parse (' main ', ' main ');
$tpl->fastprint (' MAIN ');
?>
Here we are using the popular Fasttemplate template class, but the basic idea is the same for many other template classes. First you instantiate a class, tell it where to find the template file and which template file corresponds to which part of the page, then generate the page content, give the result the identifier of the content, then, in turn, parse each template file, the template class will perform the necessary substitution operations, and finally output the parsing results to the browser.
This file is composed entirely of PHP code, does not contain any HTML code, this is its biggest advantage. Now, PHP programmers can focus on writing the code that generates the page content without having to worry about how to generate HTML to properly format the final page.
You can use this method and the above file to construct a complete Web site. If the PHP code is based on the query string in the URL to generate the page content, such as http://www.foo.com/example.php?article=099, you can construct a complete magazine website accordingly.
It is easy to see that there is a second advantage to using templates. As shown in the example above, the navigation bar on the left side of the page is saved as a single file, and we can change the navigation bar to the left of all pages of the site by simply editing the template file.
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