Template framework for making static sites with PHP
The template can improve the structure of the website. This article describes how to use a template control page layout in a website composed of a large number of static HTML pages through a new function and template class. Outline: ========================================================================================================================================================================================================】 =================================== Separation function and layout first let's take a look at the two main two main application templates purpose:
Separation Function (PHP) and Layout (HTML) Avoiding Page Elements The first purpose is to talk about the most purpose, it envisages the situation: a group of programmers write PHP scripts used to generate page content, and another set of designs The staff design HTML and graphics to control the final appearance of the page. The basic idea of separation function and layout is to make the two groups of people can write and use independent set of files: programmers only need to care about files that contain only PHP code, do not need to care about the appearance of the page; and page designers can use themselves The most familiar visual editor design page layout, there is no need to worry about destroying any PHP code embedded to the page. If you have seen a few tutorials about the PHP template, then you should have already understood the working mechanism of the template. Consider a simple page part: Above the page is the header, the left is the navigation bar, the rest is the content area. This website can have the following template file:
{Header} td> tr> | |
{leftnav} td> | {content} td> tr> table> body> html> <-! header.htm -> <-! leftnav.htm -> Foo < A href = "bar"> bar can be seen 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 marker "{}" inside is the content placeholder. The main benefit of using templates is that interface designers can edit these files in their own will, such as setting font, modifying colors, and graphics, or completely changing the layout of the page. Interface designers can edit these pages with any common HTML editor or visualization 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. In general, 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 result to the identifier of the content; 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, PHP programmers can focus on writing code that generates page content without having to format the final page for how to generate HTML to correctly format the final page. 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, such as http://www.foo.com/example.php?article=099, you can construct a complete magazine website. 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. Avoiding page elements repeating "This is really good", you might think, "My website is mainly composed of a large number of static pages. Now I can remove their public part from all pages, and I want to update these public parts. Too much trouble. After I can make a unified page layout that is easy to maintain with template. "But things are not as simple as it," 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 uses the query string in the URL to dynamically construct the page from the information source such as the database. The websites in most people in us do not necessarily have 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 representing the content in each page to the appropriate page content. 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 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 There are three questions: We must copy these complex to each page, involving the PHP code of the template, which makes the page difficult to maintain like repeating public page elements; now the file has mixed HTML and PHP code; assigning the content variable It is 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 framework for static sites 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; next to each page Three lines of PHP code as follows: Php Php request ('prepend.php');?> Php pagestart ('Home');?> Hello h1>Welcome to 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. In addition, since the HTML content is outside the PHP tag, there is no processing problem of special characters. 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. 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 ');}?> The Pagestart function first creates and sets a template instance, and then enable the output cache. Thereafter, all HTML content from the page itself will enter the cache. The PageFinish function takes out the contents in the cache, then specify these content in the template object, finally parsing the template and outputs the finished 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. This article downloads the package contains a running example website, which is more detailed than the previous code annotation. The FastTemplate class can be found at http://www.thewebmasters.net/, the latest version number is 1.1.0, there is a small patch to ensure that the class is properly run in PHP 4. The classes in this article download code have been corrected by this patch.
转载请注明原文地址:https://www.9cbs.com/read-126634.html New Post(0)
|