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

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

WBOY
Release: 2016-07-21 16:06:26
Original
917 people have browsed it

The PHP code is all saved into a separate file, which is the file actually called by the page URL. The web server parses the file through the PHP engine and returns the results to the browser. Generally, PHP code always dynamically generates page content, such as querying a database or performing certain calculations. Here is an example:



// example.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
' leftnav' => 'leftnav.htm' ) );

// The PHP code here sets $content to contain 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 use the popular FastTemplate template class, but the basic idea is the same for many other template classes. First, you instantiate a class and tell it where to find template files and which template file corresponds to which part of the page; then, generate the page content and assign the result to the content identifier; then, parse each template file in turn, The template class will perform the necessary replacement operations; finally, the parsing results will be output to the browser.


This file is entirely composed of PHP code and does not contain any HTML code. This is its biggest advantage. Now, PHP programmers can focus on writing the code that generates the content of the page, rather than worrying about how to generate the HTML to properly format the final page.


You can use this method and the above files to construct a complete website. If the PHP code generates page content based on the query string in the URL, such as http://www.foo.com/example.php?article=099, you can construct a complete magazine website based on this.


It’s easy to see that there is a second benefit to using templates. As shown in the example above, the navigation bar on the left side of the page is saved as a separate file. We only need to edit this template file to change the navigation bar on the left side of all pages of the website.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315436.htmlTechArticleThe PHP code is all saved in a separate file. This file is the file actually called by the page URL. The web server parses the file through the PHP engine and returns the results to the browser. ...
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