Template framework for making static Web sites in PHP (iv)
Source: Internet
Author: User
KeywordsTemplate framework for making static Web sites in PHP (iv)
Template framework for static web sites
First, we write the template file for all the common elements of the page and the overall layout of the page as before, then delete the public section from all the pages, leaving only the page content, and then add three lines of PHP code to each page, as follows:
How are you doing
Welcome to visit
Hope you can like this website
?>
This approach basically solves the various problems mentioned earlier. Now there are only three lines of PHP code in the file, and no line of code directly touches the template, so it is very unlikely to change the code. In addition, because the HTML content is outside the PHP tag, there is no processing problem with special characters. 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 necessary PHP code related to the template. Where the Pagestart function sets the template object as well as the page title, the Pagefinish function resolves the template and generates the result to send to the browser.
How is this implemented? Why is the HTML in the file not sent to the browser before calling the Pagefinish function? The answer lies in a new PHP 4 feature that allows the content of the output to the browser to be intercepted into the buffer. Let's take a look at the specific code for prepend.php:
Require (' class. Fasttemplate.php ');
function Pagestart ($title = ") {
GLOBAL $tpl;
$TPL = new Fasttemplate ('. ');
$TPL->define (' 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.