13.2 模板系统
Templating Systems
模板系统(Templating System)可以使网页外观布局和网页的功能代码分离。在大型项目中,模板可以使设计师专注于设计页面,而程序员专注于编程。模板系统的基本思想就是网页包含特殊的标签,然后用程序产生的动态内容去替换这些标签。设计师可以仅负责生成HTML页面,只需要考虑布局及适合的标签来读取需要的动态内容。而程序员则负责生成动态内容。
为了更具体地说明,让我们看一个简单的例子。下面的网页要求用户提交一个名字,如果已经提交,就向用户致谢:
<html>
<head>
<title>User Information</title>
</head>
<body>
<?php if (!empty($_GET['name'])) {
//根据提交的值进行处理
?>
<p><font face="helvetica,arial">Thank you for filling out the form,
<?php echo $_GET['name'] ?>.</font></p>
<?php }
else { ?>
<p><font face="helvetica,arial">Please enter the
following information:</font></p>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
</table>
</form>
<?php } ?>
</body>
</html>
在各种布局标签如font和table中放置不同的PHP元素,这个任务最好交给设计师来完成,特别在页面比较复杂的情况下。使用模板系统,我们可以把页面分为几个文件,有的包含PHP代码,有的则包含页面布局。这样HTML页面中就会包含将被动态内容替代的特殊标签。示例13-1显示了这个新的HTML模板,它被存为user.template文件。模板使用了{DESTINATION}标签来指定处理表单的脚本。
示例13-1:用户输入表单的HTML模板
<html>
<head>
<title>User Information</title>
</head>
<body>
<p><font face="helvetica,arial">Please enter the following
information:</font></p>
<form action="{DESTINATION}">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
</table>
</form>
</body>
</html>
示例13-2列出了感谢页面的模板,文件为thankyou.template,这个页面在用户填写表单之后显示。这个模板用了{NAME}标签来包含用户名的值。
示例13-2:感谢页面的HTML模板
<html>
<head>
<title>Thank You</title>
</head>
<body>
<p><font face="helvetica,arial">Thank you for filling out the form,
{NAME}.</font></p>
</body>
</html>
现在我们需要一个脚本来处理模板页面,为各种不同的标签填入相应的信息。示例13-3列出了使用这些模板的PHP脚本(一个在用户给我们信息之前,一个在之后)。PHP代码使用了FillTemplate( )函数来结合变量值和模板文件。这个文件名是form_template.php.
示例13-3:模板脚本
$bindings['DESTINATION'] = $PHP_SELF;
$name = $_GET['name'];
if (!empty($name)) {
//根据提交的值进行处理
$template = "thankyou.template";
$bindings['NAME'] = $name;
}
else {
$template = "user.template";
}
echo FillTemplate($template, $bindings);
示例13-4列出了示例13-3中使用的FillTemplate( )函数的代码。这个函数有三个参数:模板文件名、一个值数组及一个可选的参数来指定当找到标签却没有相应的值时如何处理标签,如“delete”,删除标签;“comment”,把标签替换成注释;任何其他形式则表示不对标签进行处理。这个文件存为func_template.php.
示例13-4:FillTemplate( ) 函数
function FillTemplate($inName, $inValues = array( ),
$inUnhandled = "delete") {
$theTemplateFile = $_SERVER['DOCUMENT_ROOT'] . '/templates/' . $inName;
if ($theFile = fopen($theTemplateFile, 'r')) {
$theTemplate = fread($theFile, filesize($theTemplateFile));
fclose($theFile);
}
$theKeys = array_keys($inValues);
foreach ($theKeys as $theKey) {
// 查找并替换模板中心所有关键字
$theTemplate = str_replace("\{$theKey}", $inValues[$theKey],
$theTemplate);
}
if ('delete' == $inUnhandled ) {
// 删除剩下的关键字
$theTemplate = eregi_replace('{[^ ]]*)', '', $theTemplate);
} elseif ('comment' == $inUnhandled ) {
// 对剩下的关键字进行注释
$theTemplate = eregi_replace('{([^ ])*})', '<!-- \\1 undefined -->',
$theTemplate);
}
return $theTemplate;
}
很明显,这个模板系统的例子只是概念性的,有点做作。但是如果你开发的是一个大型的PHP应用,需要显示几百篇新闻文章,你可以想像使用{HEADLINE}、{BYLINE}和{ARTICLE}这些标签的模板系统是多么实用,它可以让设计师在设计页面时不需要担心新闻的实际内容。
在模板减少设计师接触的PHP代码量的同时,系统性能会受一定影响。因为每个请求都要付出从模板创建页面的代价,同时对每个页面都进行标签的模式匹配也会使网站变慢。 Andrei Zmievski创建的Smarty模板引擎是一个高效的模板系统,很好地避开了我们的性能问题。Smarty把模板转化为PHP代码并进行缓存。它只有在模板文件改变的时候,才重新构建页面,而不是在每个请求发生的时候都构建页面。 更多信息请参见 http://smarty. php.net/。







