Creating a template framework for static web sites with PHP (iv)
Source: Internet
Author: User
Template framework for static web sites
First, we write template files for all the common elements of the page as well as the overall layout of the page, and then remove the public parts from all the pages, leaving only the content of the page, followed by adding three lines of PHP code to each page, as follows:
This approach basically solves the various problems mentioned earlier. Now there are only three lines of PHP code in the file, and no single line of code is directly involved in the template, so it's very unlikely to change the code. In addition, because the HTML content is outside the PHP tag, there is no special character handling 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 of the required PHP code associated with the template. Where the Pagestart function sets the template object and the page title, the Pagefinish function parses the template and then generates the results to the browser.
How did this happen? Why is the HTML in the file not sent to the browser before calling the Pagefinish function? The answer is a new feature of PHP 4 that allows you to intercept output to a browser in a buffer. Let's look at Prepend.php's specific code:
<?php
Require (' class. Fasttemplate.php ');
function Pagestart ($title = ' ") {
GLOBAL $tpl;
$TPL = new Fasttemplate ('. ');
$TPL->define Array (' main ' => ' main.htm '),
' Header ' => ' 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 ');
}
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