Use PHP to create a static website template framework (2)

Source: Internet
Author: User
Use PHP to create a static website template framework (2) save all PHP code to a separate file, which is the file 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.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.