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

Source: Internet
Author: User
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 SyntaxHighlighter. all (); 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 (as the mainstream development language) page can generate all pages of the entire website because it uses the query string in the URL to dynamically construct pages 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 (as the mainstream development language) 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 (as the mainstream development language) file for each page, and then use php (as the mainstream development language) on each page) the variables in the code that represent the content are set to the appropriate page content. 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 (as the mainstream development language)
Require (class. FastTemplate. php (as the mainstream development language ));
$ Tpl = new FastTemplate (.);
$ Tpl-> define (array (main => main.htm,
Header => 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 complex php code involving the template (as the mainstream development language) for every page, similar to repeated public page elements, this makes the page difficult to maintain; now the file is mixed with HTML and php (as the mainstream development language) code; assigning values to content variables will become very difficult, because we must handle a large number of special characters.


The key to solving this problem is to separate php (as the mainstream development language) code and HTML content. although we cannot delete all HTML content from the file, however, most php code (as the mainstream development language) can be removed.

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.