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 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
Make it contain 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.

Related Article

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