使用 PHP 包含分隔网站内容
我正在寻求有关将站点内容分成逻辑块的最佳实践的建议.我想要一个在整个站点中保持不变的页眉和页脚,这样如果我有几个不同内容的页面,它们都会如下所示——对页眉和页脚所做的更改然后自动更新,而我不必更改每个单独的页面.
<身体><p>页面内容在此</p>
<?包括'footer.php';?>
header.php
将包含开头的 、
和静态内容,以及
footer.php
将包含任何额外的静态内容和结束 </html>
标签.所以,我的问题是:这是一个好方法吗?我担心将 标签散布在多个文件中是不好的做法.如果是这样,处理这种设计的正确方法是什么?
解决方案
不,你的方法是错误的.
以下是您设计中的主要缺陷:
每个人都必须牢记的主要规则:
在所有数据准备就绪之前,不必将任何字符发送到浏览器中.
为什么?
HTTP header
.有时我们必须发送它们.如果您已经发送了华丽的 HTML 标头,这是不可能的.
标签.这不是很常见的事情吗?但是如果不使用模板,你就无法做到.因此,您必须拥有一个包含页眉和页脚的通用站点模板,以及每个 php 脚本的专用模板.
一个示例布局将是这样的:
.1.页面本身.
它什么都不输出,但只收集所需的数据并调用模板:
.2.template.php
这是您的主要站点模板,
由页眉和页脚组成:
<头><title>我的网站.<?=$pagetitle?></title>头部><身体><div id="页面"><?php 包含 $tpl ?>
.3.最后 links.tpl.php
是实际的页面模板:
=$pagetitle?>
<ul><?php foreach($DATA as $row): ?><li><a href="<?=$row['link']?>"target="_blank"><?=$row['name']?></a></li><?php endforeach ?><ul>
简单、干净且易于维护.
I'm looking for advice on the best practice for separating site content up into logical blocks. I want a header and footer that are constant throughout the site, so that if I have several pages of different content, they will all look as below — changes made to the header and footer then update automatically without me having to change each individual page.
<?php
include 'header.php';
?>
<body>
<p>page content here</p>
</body>
<?
include 'footer.php';
?>
The header.php
would contain the opening <html>
, <head>
and static content, and the footer.php
would contain any extra static content and the closing </html>
tag. So, my question is: Is this a good approach? I'm worried that spreading the <html>
tags across multiple files is bad practice. If so, what is the right way to approach this kind of design?
解决方案
Nope, your approach is wrong.
Here are main faults in your design:
The main rule everyone have to learn by heart:
Not a single character has to be sent into browser, until all data gets ready.
Why?
HTTP header
. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.<title>
tag for your pages, based on the page content. Isn't it extremely common thing? But you can't make it without using templates. So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
An example layout is going to be like this:
.1. page itself.
it outputs nothing but only gather required data and calls a template:
<?php
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
$pagetitle = "Links to friend sites";
//etc
//and then call a template:
$tpl = "links.tpl.php";
include "template.php";
?>
.2. template.php
which is your main site template,
consists of your header and footer:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<?php include $tpl ?>
</div>
</body>
</html>
.3. and finally links.tpl.php
is the actual page template:
<h2><?=$pagetitle?></h2>
<ul>
<?php foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<?php endforeach ?>
<ul>
easy, clean and maintainable.
相关文章