Use PHP to create a static website template framework (4)

Source: Internet
Author: User
Use PHP to create a static website template framework (4) static website template framework

First, we will write a template file for all the common page elements and the overall layout of the page as before. then, we will delete the public part from all the pages, leaving only the page content; next, add three lines of PHP code to each page, as shown below:







Hi!

Welcome



Hope you like this website





?>

This method basically solves the various problems mentioned above. Currently, there are only three lines of PHP code in the file, and no line of code is directly related to the template. Therefore, there is little possibility to modify the code. In addition, because the HTML content is outside the PHP tag, there is no special character processing problem. We can easily add these three lines of PHP code to all static HTML pages.

The require function introduces a PHP file that contains all the required PHP code related to the template. The pageStart function sets the template object and page title. the pageFinish function parses the template and generates the result and sends it to the browser.

How is this implemented? Why does the HTML in the file not be sent to the browser before calling the pageFinish function? The answer is a new function of PHP 4 that allows the interception of the content output to the browser into the buffer zone. Let's take a look at the specific code of prepend. php:


Require ('class. FastTemplate. php ');

Function pageStart ($ title = ''){
GLOBAL $ tpl;
$ Tpl = new FastTemplate ('.');
$ Tpl-> define (array ('main' => 'main.htm ',
'Head' => 'header.htm ',
'Leftnav' => 'leftnav.htm '));
$ Tpl-> assign ('title', $ TITLE );
Ob_start ();
}

Function pageFinish (){
GLOBAL $ tpl;
$ Content = ob_get_contents ();
Ob_end_clean ();
$ Tpl-> assign ('content', $ CONTENT );
$ Tpl-> parse ('header', 'header ');
$ Tpl-> parse ('leftnav', 'leftnav ');
$ Tpl-> parse ('main', 'main ');
$ Tpl-> FastPrint ('main ');
}

?>

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.