5.2.2 使用require()制作Web站点的模板
如果Web页面具有一致的外观,可以在PHP中使用require()语句将模板和标准元素加入到页面中。
例如,一个虚构的TLA咨询公司的网站有许多页面,这些页面的外观看上去都如图5-2所示。当需要一个新页面的时候,开发人员可以打开一个已有页面,从文件中间剪切所需的文本,输入所需的新文本,然后以新的文件名保存。
图 5-2 TLA咨询公司网页有着统一的外观和风格
考虑这种情况:网站已经存在了一段时间,如今已有数十个、数百个甚至数千个页面都是同一种风格。现在,要对标准外观进行部分修改——这种修改可能是很微小的改变,例如,在每个脚注上加一个电子邮件地址,或者加上一个新的导航菜单入口。你希望对这数十个、数百个甚至数千个页面都做这种微小的修改吗?
相对于剪切粘贴数十个、数百个甚至数千个页面,直接重用各个页面中通用的HTML代码部分是一个更好的办法。程序清单5-1给出了图5-2所示页面(home.html)的源代码。
程序清单5-1 home.html——TLA咨询公司主页的HTML脚本
<html>
<head>
<title>TLA Consulting Pty Ltd</title>
<style type="text/css">
h1{color:white;font-size:24pt;text-align:center;
font-family:arial,sans-serif}
.menu{color:white;font-size:12pt;text-align:center;
font-family:arial,sans-serif;font-weight:bold}
td{background:black}
p{color:black;font-size:12pt;text-align:justify;
font-family:arial,sans-serif}
p.foot{color:white;font-size:9pt;text-align:center;
font-family:arial,sans-serif;font-weight:bold}
a:link,a:visited,a:active{color:white}
</style>
</head>
<body>
<!—page header—>
<table width="100%"cellpadding="12"cellspacing="0"border="0">
<tr bgcolor="black">
<td align="left"><img src="logo.gif"alt="TLA logo"height="70"
width="70"></td>
<td>
<h1>TLA Consulting</h1>
</td>
<td align="right"><img src="logo.gif"alt="TLA logo"height="70"
width="70"></td>
</tr>
</table>
<!—menu—>
<table width="100%"bgcolor="white"cellpadding="4"cellspacing="4">
<tr>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Home</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Contact</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Services</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Site Map</span></td>
</tr>
</table>
<!—page content—>
<p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs
and hope to hear from you soon.</p>
<!—page footer—>
<table width="100%"bgcolor="black"cellpadding="12"border="0">
<tr>
<td>
<p class="foot">©TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our
<a href="legal.php">legal information page</a></p>
</td>
</tr>
</table>
</body>
</html>
在程序清单5-1中,可以看到这个文件由许多不同的代码部分组成。HTML标题包含了在该页面中用到的级联风格样式单(CSS)中的样式定义。标有"page header"的部分显示了公司的名称和徽标,标有"menu"的部分创建了页面的导航条,而标有"page content"的部分是页面中的文本。再下面的就是脚注。通常,可以将这个文件分割,然后给这些部分分别命名为header.php、home.php和footer.php。文件header.php和footer.php都包含有在其他页面中可以重用的代码。
文件home.php可以代替home.html,它包含页面内容和两个require()语句,如程序清单5-2所示。
程序清单5-2 home.php——TLA公司主页的PHP脚本
<?php
require('header.php');
?>
<!—page content—>
<p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs
and hope to hear from you soon.</p>
<?php
require('footer.php');
home.php中的require()语句将载入header.php和footer.php。
正如前面所提到的,在通过require()调用它们的时候,文件的名称并不会影响对它们的处理。一个常见的约定就是调用那些包含在其他文件something.inc(此处inc代表include)中的部分文件代码,这些文件代码若不被调用,将会停止执行。但是这却不是推荐的基本策略,因为如果Web服务器没有专门设置,.inc文件将不会被解释成PHP代码。
如果打算这样做,可以将.inc文件保存在一个目录中,而这个目录可以被脚本访问,但是被引入的文件不会被Web服务器载入——也就是,放在Web文档树之外。这种设置是非常不错的,它可以防止下面两种情况的发生:a)如果文件扩展名是.php,但只包含部分页面或脚本,此时可能会引起错误。b)如果已经使用了其他扩展名,别人就可以读取源码。
文件header.php包含了页面使用的级联风格样式单定义以及程序清单5-3所显示的公司名称和导航菜单的表格。
程序清单5-3 header.php——所有TLA网站的页面可重复使用的页眉
<html>
<head>
<title>TLA Consulting Pty Ltd</title>
<style type="text/css">
h1{color:white;font-size:24pt;text-align:center;
font-family:arial,sans-serif}
.menu{color:white;font-size:12pt;text-align:center;
font-family:arial,sans-serif;font-weight:bold}
td{background:black}
p{color:black;font-size:12pt;text-align:justify;
font-family:arial,sans-serif}
p.foot{color:white;font-size:9pt;text-align:center;
font-family:arial,sans-serif;font-weight:bold}
a:link,a:visited,a:active{color:white}
</style>
</head>
<body>
<!—page header—>
<table width="100%"cellpadding="12"cellspacing="0"border="0">
<tr bgcolor="black">
<td align="left"><img src="logo.gif"alt="TLA logo"height="70"width="70"></td>
<td>
<h1>TLA Consulting</h1>
</td>
<td align="right"><img src="logo.gif"alt="TLA logo"height="70"width="70"/></td>
</tr>
</table>
<!—menu—>
<table width="100%"bgcolor="white"cellpadding="4"cellspacing="4">
<tr>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20"/>
<span class="menu">Home</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20"/>
<span class="menu">Contact</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20"/>
<span class="menu">Services</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20"/>
<span class="menu">Site Map</span></td>
</tr>
</table>
文件footer.inc包含了在每个页面底部脚注处显示的表格。这个文件如程序清单5-4所示。
程序清单5-4 footer.php——所有TLA网站的页面可重复使用的脚注
<!—page footer—>
<table width="100%"bgcolor="black"cellpadding="12"border="0">
<tr>
<td>
<p class="foot">©TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our<a href="legal.php">
legal information page</a></p>
</td>
</tr>
</table>
</body>
</html>
这种方法很容易就使网站具有统一的风格,而且还可以通过输入如下所示的代码创建一个新的统一风格页面:
<?php require('header.php');?>
Here is the content for this page
<?php require('footer.php');?>
最重要的是,即使用这个页眉和脚注创建许多新页面后,也很容易修改页眉和脚注文件。
无论是做一个无关紧要的修改还是重新设计网站的外观,只需要进行一次修改。我们并不需要单独地对网站中的每个页面进行修改,因为它们都是载入页眉和脚注文件的。
这里,这个例子在页面正文、页眉和脚注处只使用了纯HTML,但这不是问题。有了这些文件,我们也可以用PHP命令动态地生成页面的某些部分。
如果希望保证一个文件将被当作纯文本或HTML,而且不会执行任何PHP,可以使用readfile()作为替代方法。这个函数将回显文件内容,不会对其进行解析。如果使用的是用户提供的文本,这可能就是一个重要的安全问题。