How to use PHP to create a static website template framework

Source: Internet
Author: User
The first objective is the most talked about. it is assumed that a group of programmers write PHP scripts for natural page content, at the same time, another group of design staff designed HTML and graphics to hold the ultimate appearance of the page. The first goal of the basic idea of efficacy and layout is the most talked about goal. it is assumed that a group of programmers write PHP scripts for natural page content, at the same time, another group of design staff designed HTML and graphics to hold the ultimate appearance of the page. The basic idea of efficacy and layout is to enable the two groups to write and apply independent files: programmers only need to care about files that only contain PHP code, the page design staff can design the page layout with their most familiar visual editor without worrying about damages to 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 structured by the template: the main template controls the layout of all pages; the header template and leftnav template hold the public elements of the page. The identifier in the braces "{}" is a content placeholder. The most important benefit of an application template is that the interface designer can compile these files as needed, such as setting fonts, correcting colors and graphics, or completely changing the layout of pages. Interface designers can use any common HTML editor or visualization tool to compile these pages. because these files only contain HTML code, there is no PHP code.

All PHP code is stored in 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. Generally, PHP code dynamically generates page content, such as querying a database or performing a certain calculation. 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 apply 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 are going to generate the page content and assign the result to the content identifier; then, the template files are parsed in sequence, and the template class performs the necessary switch control. 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 greatest strength. Now, PHP programmers can focus on writing code for the natural page content, instead of worrying about how to create an accurate final page for HTML.

It is easy to see that using templates has a second benefit. As shown in the preceding example, the navigation bar on the left of the page is reserved as a file. you only need to compile 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 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 page layout that is easily protected ." However, this is not so simple. "A large number of static pages" indicate the title.

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.