// example.php
require('class.FastTemplate.php');
$tpl = new FastTemplate('.');
$tpl->define( array( 'main' => 'main.htm',
'header' => 'header.htm',
'leftnav' => 'leftnav.htm' ) );
// 此处的PHP代码设置$content使其包含合适的页面内容
$tpl->assign('CONTENT', $content);
$tpl->parse('HEADER', 'header');
$tpl->parse('LEFTNAV', 'leftnav');
$tpl->parse('MAIN', 'main');
$tpl->FastPrint('MAIN');
?>
这里我们使用的是流行的FastTemplate模板类,但其基本思路对于其他许多模板类来说都一样。首先你实例化一个类,告诉它到哪里去寻找模板文件以及哪一个模板文件与页面的哪部分对应;接下来是生成页面内容,把结果赋予内容的标识符;然后,依次解析各个模板文件,模板类将执行必要的替换操作;最后把解析结果输出到浏览器。
这个文件完全由PHP代码构成,不包含任何HTML代码,这是它最大的优点。现在,PHP程序员可以集中精力编写生成页面内容的代码,而不必为了如何生成HTML去正确地格式化最终页面而担心。
你可以使用这种方法和上面的文件构造出一个完整的网站。如果PHP代码是以URL中的查询字符串为基础生成页面内容,例如http://www.foo.com/example.php?article=099,你可以据此构造出一个完整的杂志网站。
很容易看出采用模板还有第二个好处。如上例所示,页面左边的导航条单独保存为一个文件,我们只需编辑这一个模板文件就可以改变网站所有页面左边的导航条。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools