Bar < / A> You can see how the page is constructed from these templates: the main template controls the layout of the entire page; the Header template and the Leftnav template control the public elements of the page. The identifier word inside the arc random arc "{}" is a content occupying element. The main benefit of using templates is that the interface designer can edit these files in their own will, such as setting font, modifying color, and graphics, or completely changing the layout of the page. Interface designers can edit these pages with any ordinary HTML editor or visual tool, because these files contain only HTML code, without any PHP code. All PHP code is saved to a separate file, which is the file actually called by the page URL. The web server parsing the file via the PHP engine and then returns the result to the browser. Generally, PHP code always dynamically generates page content, such as querying the database or performing some calculation. Here is an example: Php // example.php require ('class.fastTemplate.php'); $ TPL = new fasttemplate ('.'); $ TPL-> Define (array ('main' => "main .htm ',' header '=>' Header.htm ',' Leftnav '=>' Leftnav.htm ')); // The PHP code setting $ Content makes it contains the right page content $ TPL-> Assign ('Content', $ Content); $ TPL-> Parse ('Header', 'Header'); $ TPL-> Parse ('Leftnav', 'Leftnav'); $ TPL-> PARSE ('main', ' Main '); $ TPL-> FASTPRINT (' main ');> Here we use the popular FastTemplate template class, but its basic ideas are the same for many other templates. First of all, you instantiate a class, tell it where to find the template file and which template file corresponds to the page; then generate the page content, give the results to the identification word; then, parse each template file, The template class will perform the necessary replacement operation; finally output the resolution result to the browser.
This file is completely constructed by PHP code, does not contain any HTML code, which is its biggest advantage. Now, the PHP programmer can focus on writing code that generates page content without having to format the final page correctly for how to generate HTML. You can use this method and the above file to construct a complete website. If the PHP code is generated by the query string in the URL, for example
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 benefit using template. As shown in the above example, the navigation strips on the left side of the page are saved as a file, and we can change the navigation bar left on the left side of the website only by editing this template file.
Avoid pages element repeat
"This is really good", you may think, "My website is mainly composed of a large number of static pages. Now I can delete their public part from all pages, and update these public part is too much trouble. I will You can make a unified page layout that is easy to maintain with a template. "But things are not as simple," a large number of static pages "said the problem.
Consider the above example. This example actually has only an example.php page, which can generate all the pages of the entire website because it utilizes the query string in the URL to dynamically construct the page dynamically from the information source such as the database.
The websites in most people in us do not have to have a database support. Most of our website consists of static pages, then use PHP here, there are some dynamic functions, such as search engines, feedback forms, etc. So how do I apply templates on this website?
The easiest way is to copy a php file for each page, and then set the variables of the PHP code to the appropriate page content in each page. For example, suppose there are three pages, they are homepage (home), About and Products, we can generate them with three files. The content of these three files is like:
php // Home.php Require ('Class.fastTemplate.php'); $ TPL = New FastTemplate ('.'); $ TPL-> Define (Array ('main' => 'main.htm', ' Header '=>' Header.htm ',' Leftnav '=>' Leftnav.htm ')); $ Content = "
Welcome to Access P>
I hope you can like this website p> "; $ TPL-> Assign ('Content', $ Content); $ TPL-> PARSE ('Header', 'Header'); $ TPL-> PARSE ('Leftnav ',' Leftnav '); $ TPL-> PARSE (' main ',' main '); $ TPL-> Fastprint (' main ');?>
Obviously, this method has three problems: we must copy these complex, involving the PHP code involved in the template, which makes the page difficult to maintain as repeating public page elements; now the file has mixed HTML and PHP code; The content variable assignment will become very difficult because we have to handle a lot of special characters. The key to solving this problem is to separate the PHP code and HTML content, although we cannot delete all HTML content from the file, but can remove most PHP code.
Template frame for static website
First, we write template files as all of the page public elements and page overall layouts, and then remove the public part from all pages, leaving only page content; then add three lines of PHP code in each page. As follows:
php PHP Require ('prepend.php');?> php pagestart ('Home');?>
Hello h1>
Welcome access p>
I hope you can like this website p> Php pagefinish ();?>?>
This method basically solves the various problems mentioned earlier. There are only three lines of PHP code in the file, and there is no row of code directly to the template, so it is necessary to change the possibility of these code. Further, since the HTML content is located outside the PHP tag, there is no processing problem of special character. We can easily add this three-line PHP code to all static HTML pages.
The Require function introduced a PHP file that contains all the required PHP code related to the template. Where the Pagestart function sets the template object and the page title, the PageFinish function parsing the template and generates the result to send it to the browser.
How is this implementation? Why is HTML in the file before calling the PageFinish function to the browser? The answer is a new feature of PHP 4, which allows the contents of the output to the browser to the buffer. Let's take a look at the specific code of prepend.php:
PHP Require ('Class.fastTemplate.php'); Function Pagestart ($ TITLE = ') {Global $ TPL; $ TPL = New FastTemplate ('. '); $ TPL-> Define (Array (' main ' => 'main.htm', 'header' => 'Header.htm', 'Leftnav' => 'Leftnav.htm'); $ TPL-> Assign ('Title', $ TITLE); OB_START (); Function Pagefinish () {Global $ TPL; $ Content = Ob_Get_Contents (); OB_END_CLEAN (); $ TPL-> Assign ('Content', $ Content); $ TPL-> Parse ('Header', 'Header'); $ TPL-> PARSE ('Leftnav', 'Leftnav'); $ TPL-> PARSE ('main', 'main'); $ TPL-> Fastprint ('main');}?> PageStart function first created and set A template instance and then cache it. Thereafter, all HTML content from the page itself will enter the cache. The PageFinish function takes out the contents of the cache and specifies these contents in the template object, finally parsing the template and outputs the completed page.
This is the entire work process of the entire template frame. First, write a template that contains all the public elements of the website, then removes all public page layout code from all pages, in which the PHP code will never need to be changed; then add the fastTemplate class file and prepend.php to the included path So you get a website that can focus on a page layout, it has better reliability and maintainability, and the large-scale modification of the site level has become quite easy.
FastTemplate class can be
Http://www.thewebmasters.net/ Find, the latest version number is 1.1.0, there is a small patch used to ensure that the class is properly run in PHP 4. The classes in this article download code have been corrected by this patch.