Home > Backend Development > PHP Tutorial > Using PHP to create a template framework for static websites (4)_PHP Tutorial

Using PHP to create a template framework for static websites (4)_PHP Tutorial

WBOY
Release: 2016-07-21 16:06:31
Original
816 people have browsed it

Template framework for static websites

First, we write template files for all common elements of the page and the overall layout of the page as before; then delete the common parts from all pages, leaving only the page content; next, in Add three lines of PHP code to each page, as follows:




< ;?php require('prepend.php'); ?>


Hello


Welcome



Hope you like this website





?>

This method basically solves the various problems mentioned earlier. There are only three lines of PHP code in the file now, and none of them directly refer to the template, so the possibility of changing this code is extremely slim. In addition, since the HTML content is outside the PHP markup, there is no problem with handling 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 template-related PHP code. The pageStart function sets the template object and page title, and the pageFinish function parses the template and generates the result and sends it to the browser.

How is this achieved? Why is the HTML in the file not sent to the browser until the pageFinish function is called? The answer lies in a new feature of PHP 4, which allows the content output to the browser to be intercepted into a buffer. 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',
'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');
}

?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315430.htmlTechArticleStatic website template framework First, we write template files for all page common elements and the overall page layout as before ;Then delete the public part from all pages, leaving only...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template