How to use PHP to create a static website template framework

Source: Internet
Author: User
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 function and layout is to enable the two groups to write and use an independent set of files: programmers only need to focus on those articles that only contain PHP code. The first purpose is to talk about the most. 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.

We can see how the page is constructed by a template: 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:

// 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.

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:

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:

ob_end_clean(); $tpl->assign('CONTENT', $content); $tpl->parse('HEADER', 'header'); $tpl->parse('LEFTNAV', 'leftnav'); $tpl->parse('MAIN', 'main'); $tpl->FastPrint('MAIN'); } ?>

The ageStart 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.

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.