Teach you how to use PHP to create a template framework for static web sites

Source: Internet
Author: User
Tags 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:

Here's the PHP code setting $content
Make it contain the appropriate page content





?>

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.

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.