Using PHP to create a static website template framework template can improve the website structure. This article describes how to use a new function and template class of PHP 4 to skillfully control page layout in a website composed of a large number of static HTML pages.
Outline:
==========================================
Separation of functions and layout
Avoid repeated page elements
Static website template framework
==========================================
Separation of functions and layout
First, let's take a look at the two main purposes of the application template:
Separation of functions (PHP) and layout (HTML)
Avoid repeated page elements
The first purpose is to talk the most about it. it assumes that a group of programmers write PHP scripts used to generate page content, at the same time, another group of designers designed HTML and graphics to control the final appearance of the page. The basic idea of separation of functions and layout is to enable the two groups to write and use independent files: programmers only need to care about files that only contain PHP code, the page designer can design the page layout with the most familiar visual editor without worrying about damaging any PHP code embedded into the page.
If you have read several tutorials on PHP templates, you should have understood the working mechanism of the template. Consider a simple page: the top of the page is the page header, the left is the navigation bar, and the rest is the content area. Such websites can have the following template files:
Template example
{HEADER} |
{LEFTNAV} |
{CONTENT} |
Foo
Bar
We can see how the page is constructed by these templates: the main template controls the layout of the entire page; the header template and leftnav Template control the public elements of the page. The identifier in the braces "{}" is a content placeholder. The main advantage of using a template is that the interface designer can edit these files as needed, such as setting fonts, changing colors and graphics, or completely changing the page layout. The interface designer can use any common HTML editor or visualization tool to edit these pages, because these files only contain HTML code, without any PHP code. All PHP code is saved to a separate file. This file is actually called by the page URL. The Web server parses the file through the PHP engine and returns the result to the browser. In general, PHP code always dynamically generates page content, such as querying a database or executing a computing task. The following is an example:
// Example. php
Require ('class. FastTemplate. php ');
$ Tpl = new FastTemplate ('.');
$ Tpl-> define (array ('main' => 'main.htm ',
'Head' => 'header.htm ',
'Leftnav' => 'leftnav.htm '));
// The PHP code here sets $ 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 use the popular FastTemplate template class, but its basic idea is the same for many other template classes. First, you instantiate a class and tell it where to find the template file and which template file corresponds to the part of the page. Next, you generate the page content and assign the result to the content identifier; the template files are parsed in sequence, and the template class performs the necessary replacement operations. Finally, the parsing result is output to the browser.
This file is completely composed of PHP code and does not contain any HTML code. this is its biggest advantage. Now, PHP programmers can focus on writing code that generates page content without worrying about how to generate HTML to correctly format the final page.
You can use this method to construct a complete website with the above files. If PHP code generates page content based on query strings in URLs, 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 benefit to using templates. As shown in the preceding example, the navigation bar on the left of the page is saved as a file. you only need to edit this template file to change the navigation bar on the left of all pages of the website. Avoid repeated page elements
"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 parts from all pages. it is too much trouble to update these public parts. In the future, I can use a template to create a uniform page layout that is easy to maintain ." But it is not that simple. "A large number of static pages" indicate the problem.
Consider the example above. In this example, there is actually only one example. php page. it can generate all the pages of the entire website because it uses the query string in the URL to dynamically construct the page from information sources such as databases.
Most of us do not necessarily have Database support for their websites. Most of our websites are composed of static pages, and PHP is used to add dynamic functions, such as search engines and feedback forms. So, how to apply templates on such websites?
The simplest way is to copy a PHP file for each page, and then set the variables representing the content in PHP code to the appropriate page content in each page. For example, suppose there are three pages: home, about, and product. we can use three files to generate them respectively. The content of these three files is similar:
// Home. php
Require ('class. FastTemplate. php ');
$ Tpl = new FastTemplate ('.');
$ Tpl-> define (array ('main' => 'main.htm ',
'Head' => 'header.htm ',
'Leftnav' => 'leftnav.htm '));
$ Content ="
Welcome
Hope you like this website
";
$ 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 complicated PHP code involving the template for each page, which makes the page difficult to maintain just like repeated public page elements; now the file is mixed with HTML and PHP code. it is very difficult to assign values to content variables because we have to deal with a large number of special characters.
The key to solving this problem lies in the separation of PHP code and HTML content. although we cannot delete all HTML content from the file, we can remove the vast majority of PHP code. Static website template framework
First, we will write a template file for all the common page elements and the overall layout of the page as before. then, we will delete the public part from all the pages, leaving only the page content; next, add three lines of PHP code to each page, as shown below:
Hi!
Welcome
Hope you like this website
?>
This method basically solves the various problems mentioned above. Currently, there are only three lines of PHP code in the file, and no line of code is directly related to the template. Therefore, there is little possibility to modify the code. In addition, because the HTML content is outside the PHP tag, there is no special character processing problem. We can easily add these three lines of PHP code to all static HTML pages.
The require function introduces a PHP file that contains all the required PHP code related to the template. The pageStart function sets the template object and page title. the pageFinish function parses the template and generates the result and sends it to the browser.
How is this implemented? Why does the HTML in the file not be sent to the browser before calling the pageFinish function? The answer is a new function of PHP 4 that allows the interception of the content output to the browser into the buffer zone. Let's take a look at the specific code of prepend. php:
Require ('class. FastTemplate. php ');
Function pageStart ($ title = ''){
GLOBAL $ tpl;
$ Tpl = new FastTemplate ('.');
$ Tpl-> define (array ('main' => 'main.htm ',
'Head' => '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 ');
}
?> The pageStart function first creates and sets a template instance, and then enables the output cache. After that, all HTML content from the page itself will be cached. The pageFinish function retrieves the cached content, specifies the content in the template object, parses the template, and outputs the completed page.
This is the entire template framework process. First, write a template containing the public elements of each page of the website, and then delete all public page layout code from all pages, replacing it with three lines of PHP code that will never be changed; then, set the FastTemplate class file and prepend. add php to the inclusion path, so that you can get a website with centralized control of the page layout. it has better reliability and maintainability, and it also becomes quite easy to modify a wide range of websites.
The downloaded package contains a running sample website. its code comments are more detailed than the previous code comments. The FastTemplate class can be found at http://www.thewebmasters.net/. the latest version number is 1.1.0. There is also a small patch used to ensure that the class runs correctly in PHP 4. The classes in the downloaded code in this article have been corrected by the patch.