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 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:


<? Php

// Home. php
Require ('class. FastTemplate. php ');
$ Tpl = new FastTemplate ('.');
$ Tpl-> define (array ('main' => 'main.htm ',
'Head' => 'header.htm ',
'Leftnav' => 'leftnav.htm '));

$ Content = "<p> welcome </p>

<P> hope you 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 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.

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.