How to create a template framework for static web sites with PHP

Source: Internet
Author: User
Tags final header include variables php file php code advantage
The first is to talk about the most, and it envisions a scenario where a group of programmers write PHP scripts to generate page content, while another group of designers design HTML and graphics to control the final appearance of the page. The basic idea of separating functions and layouts is to enable both groups to write and use separate sets of files: programmers only care about files that contain only PHP code, without caring about the appearance of the page, and page designers can design page layouts with their most familiar visual editors, No need to worry about breaking any PHP code embedded in the page.

If you've ever seen a few tutorials about PHP templates, you should already understand how the templates work. Consider a simple part of the page: the top of the page is the header, the left is the navigation bar, and the rest is the content area.

You can see how a page is constructed from a template: The main template controls the layout of the entire page; the header and LeftNav templates control the common elements of the page. The identifier inside the curly brace "{}" is a content placeholder. The main advantage of using templates is that interface designers can edit them as they wish, such as setting fonts, modifying colors and graphics, or completely changing the layout of a page. Interface designers can edit these pages with any normal HTML editor or Visual tool, because they contain only HTML code and no PHP code.

The PHP code is saved in a separate file, which is the file that is actually called by the page URL. The Web server parses the file through the PHP engine and returns the results to the browser. Generally, PHP code always dynamically generates page content, such as querying a database or performing some sort of calculation. Here is an example:

The PHP code here is set $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 are using the popular Fasttemplate template class, but the basic idea is the same for many other template classes. First you instantiate a class, tell it where to find the template file and which template file corresponds to which part of the page, then generate the page content, give the result the identifier of the content, then, in turn, parse each template file, the template class will perform the necessary substitution operations, and finally output the parsing results to the browser.

This file is composed entirely of PHP code, does not contain any HTML code, this is its biggest advantage. Now, PHP programmers can focus on writing the code that generates the page content without having to worry about how to generate HTML to properly format the final page.

It is easy to see that there is a second advantage to using templates. As shown in the example above, the navigation bar on the left side of the page is saved as a single file, and we can change the navigation bar to the left of all pages of the site by simply editing the template file.

Avoid page element duplication

"It's really good," You might think, "My site is mostly composed of a lot of static pages." Now that I can remove their public parts from all the pages, it's too much trouble to update the public parts. In the future, I can create a unified page layout that is easy to maintain with templates. "But things are not so simple," a lot of static pages say the problem lies.

Please consider the above example. This example actually has only one example.php page, and it is able to generate all the pages of the entire site because it uses the query string in the URL to dynamically construct the page from information sources such as databases.

Most of us are running websites that don't necessarily have database support. Most of our site is composed of static pages, and then use PHP here, there are some dynamic features, such as search engines, feedback forms. So how do you apply a template on such a Web site?

The easiest way to do this is to copy a PHP file for each page, and then set the variables in the PHP code that represent the content in each page to the appropriate page content. For example, suppose you have three pages that are home, about (about), and products (product), and we can generate them separately with three files. The contents of these three files are as follows:

<p> hope you can enjoy this website </p> "; $tpl->assign (' CONTENT ', $content); $tpl->parse (' header ', ' header '); $tpl->parse (' LeftNav ', ' leftnav '); $TPL->parse (' main ', ' main '); $tpl->fastprint (' MAIN ');?>

Obviously, there are three problems with this approach: we have to replicate these complex, template-related PHP code for each page, which is as difficult to maintain as repeating common page elements; now the files are mixed with HTML and PHP code; Assigning values to content variables becomes very difficult, Because we have to deal with a lot of special characters.

The key to solving this problem is to separate the PHP code from the HTML content, although we can't remove all the HTML content from the file, but we can move out most of the PHP code.

Template framework for static Web sites:

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 output caching. Thereafter, all HTML content from the page itself will go into the cache. The Pagefinish function takes out the contents of the cache and then specifies the contents in the template object, finally parsing the template and outputting the completed page.

This is the entire template framework of the whole process of work. First write the template that contains the common elements of each page of the site, and then remove all the common page layout codes from all the pages and replace them with three lines of PHP code that never changes. and add the Fasttemplate class file and prepend.php to the include path, so you get a Web site where the page layout can be centrally controlled, it has better reliability and maintainability, and extensive changes at the site level become quite easy.

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.