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?
的头。php将包含开头的、和静态内容以及页脚。php将包含任何额外的静态内容和关闭的标记。所以,我的问题是:这是一个好方法吗?我担心在多个文件中传播标记是不好的做法。如果是这样的话,什么样的设计才是正确的呢?
4 个解决方案
#1
13
Nope, your approach is wrong.
Here are main faults in your design:
不,你的方法是错误的。以下是你设计中的主要缺点:
- You're assuming that header.php would be called on the every page call. That's wrong.
- 你假设头。每个页面调用都会调用php。这是错误的。
- You're assuming that header.php will always be static. That's wrong.
- 你假设头。php总是静态的。这是错误的。
- You forgot to create a template for the page itself.
- 您忘记为页面本身创建模板了。
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?
为什么?
- it's 2011 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
- 今天是2011年。AJAX的时代。如果您的代码必须发送json数据而不是整个HTML页面怎么办?
- there is a thing called
HTTP header
. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent. - 有一个东西叫做HTTP头。有时我们不得不寄给他们。如果你已经发送了华丽的HTML头,这是不可能的。
- it's for just 4-page site. Okay. Imagine you've got lucky and got a request for another 4-page site. You will have to change only templates and don't touch engine files. That's really great benefit.
- 只有4页的网站。好吧。假设你很幸运,收到了另一个4页的请求。您将必须只更改模板,并且不要触摸引擎文件。这是真正伟大的利益。
- Imagine you're going to make a custom
<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. -
假设您将根据页面内容为您的页面创建一个自定义的
标记。这不是很常见吗?但是,如果不使用模板,就无法创建它。</li>
So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
因此,您必须拥有一个包含页眉和页脚的公共站点模板,以及每个php脚本的专用模板。
An example layout is going to be like this:
一个示例布局是这样的:
.1. page itself.
1。。页面本身。
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,
。2。模板。php是你的主要网站模板,
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:
。3。最后links.tpl。php是实际的页面模板:
<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.
简单,清洁和维护。
#2
7
In building off of Your Common Sense
's answer, there's not a good reason to have 2 files for every page. You can easily combine your template (YCS called this .tpl.php) and your actual page into one file.
基于常识的答案,没有一个好的理由让每一页都有两个文件。您可以轻松地将模板(YCS称为.tpl.php)和实际页面合并到一个文件中。
First, start off with a class that you can expand as your template needs expand:
首先,从一个类开始,当您的模板需要扩展时,您可以扩展这个类:
<?php
#lib/PageTemplate.php
class PageTemplate {
public $PageTitle;
public $ContentHead;
public $ContentBody;
}
Then, make your layout:
然后,让你的布局:
<?php
# layout.php
require_once('lib/PageTemplate.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<title><?php if(isset($TPL->PageTitle)) { echo $TPL->PageTitle; } ?></title>
<?php if(isset($TPL->ContentHead)) { include $TPL->ContentHead; } ?>
</head>
<body>
<div id="content">
<?php if(isset($TPL->ContentBody)) { include $TPL->ContentBody; } ?>
</div>
</body>
</html>
And finally, add your page with the body content:
最后,在页面中加入正文内容:
<?php
#Hello.php
require_once('lib/PageTemplate.php');
# trick to execute 1st time, but not 2nd so you don't have an inf loop
if (!isset($TPL)) {
$TPL = new PageTemplate();
$TPL->PageTitle = "My Title";
$TPL->ContentBody = __FILE__;
include "layout.php";
exit;
}
?>
<p><?php echo "Hello!"; ?></p>
#3
4
This is a basic approach but, yeah, it does work :) I sure would bother with a lot of templating and OOP but you are definitely on the right path
这是一种基本的方法,但是,是的,它确实有用:)我肯定会为大量的模板和OOP而烦恼,但您肯定走对了路
As i can't comment anymore, then i will answer here ;) If he need a custom title then he needs some more advanced functions. So, as i told, this is a basic approach. But in the end, if he really have a static header/footer, and really use them everywhere, well, yes, this is a good way to go.
因为我不能再评论了,所以我在这里回答;如果他需要一个自定义标题,那么他需要一些更高级的功能。正如我所说,这是一个基本的方法。但是最后,如果他真的有一个静态的页眉/页脚,并且在任何地方都使用它们,那么,是的,这是一个很好的方法。
So ofc you could bother with some advanced headers with parameters you could feed on each page. You could go on a whole MVC stuff. In the end just tell him to use a pre-made framework and stop bothering. How could he learn if you don't let him do some trial and error ?
所以ofc你可以在每个页面上添加一些高级标题和参数。你可以使用整个MVC。最后,告诉他使用一个预先准备好的框架,不要再麻烦了。如果你不让他反复试验,他怎么可能学会呢?
#4
-6
index.php -- includes header, footer, and content based on REQUEST variable.
header.php -- header content
footer.php -- footer content
索引。php——包括基于请求变量的页眉、页脚和内容。头。php——页眉内容页脚。php,页脚内容
content1.php, content2.php, etc.
content1。php,content2。php等。
index.php:
index . php:
<?php
include ('header.php');
// VERY IMPORTANT - do not use the GET variable directly like this
// make sure to filter it through a white-list
include(basename($_GET['page']).'.php');
include ('footer.php');
?>
if you want the URL to go www.domain.com/pagename where the page you're trying to load into index.php is "pagename", use HTACCESS and do some URL Rewriting: http://corz.org/serv/tricks/htaccess2.php
如果您想要将URL转到www.domain.com/pagename,则需要将页面加载到index中。php是“pagename”,使用HTACCESS并做一些URL重写:http://corz.org/serv/phints/htaccess2.php
#1
13
Nope, your approach is wrong.
Here are main faults in your design:
不,你的方法是错误的。以下是你设计中的主要缺点:
- You're assuming that header.php would be called on the every page call. That's wrong.
- 你假设头。每个页面调用都会调用php。这是错误的。
- You're assuming that header.php will always be static. That's wrong.
- 你假设头。php总是静态的。这是错误的。
- You forgot to create a template for the page itself.
- 您忘记为页面本身创建模板了。
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?
为什么?
- it's 2011 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
- 今天是2011年。AJAX的时代。如果您的代码必须发送json数据而不是整个HTML页面怎么办?
- there is a thing called
HTTP header
. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent. - 有一个东西叫做HTTP头。有时我们不得不寄给他们。如果你已经发送了华丽的HTML头,这是不可能的。
- it's for just 4-page site. Okay. Imagine you've got lucky and got a request for another 4-page site. You will have to change only templates and don't touch engine files. That's really great benefit.
- 只有4页的网站。好吧。假设你很幸运,收到了另一个4页的请求。您将必须只更改模板,并且不要触摸引擎文件。这是真正伟大的利益。
- Imagine you're going to make a custom
<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. -
假设您将根据页面内容为您的页面创建一个自定义的
标记。这不是很常见吗?但是,如果不使用模板,就无法创建它。</li>
So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.
因此,您必须拥有一个包含页眉和页脚的公共站点模板,以及每个php脚本的专用模板。
An example layout is going to be like this:
一个示例布局是这样的:
.1. page itself.
1。。页面本身。
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,
。2。模板。php是你的主要网站模板,
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:
。3。最后links.tpl。php是实际的页面模板:
<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.
简单,清洁和维护。
#2
7
In building off of Your Common Sense
's answer, there's not a good reason to have 2 files for every page. You can easily combine your template (YCS called this .tpl.php) and your actual page into one file.
基于常识的答案,没有一个好的理由让每一页都有两个文件。您可以轻松地将模板(YCS称为.tpl.php)和实际页面合并到一个文件中。
First, start off with a class that you can expand as your template needs expand:
首先,从一个类开始,当您的模板需要扩展时,您可以扩展这个类:
<?php
#lib/PageTemplate.php
class PageTemplate {
public $PageTitle;
public $ContentHead;
public $ContentBody;
}
Then, make your layout:
然后,让你的布局:
<?php
# layout.php
require_once('lib/PageTemplate.php');
?>
<!DOCTYPE HTML>
<html>
<head>
<title><?php if(isset($TPL->PageTitle)) { echo $TPL->PageTitle; } ?></title>
<?php if(isset($TPL->ContentHead)) { include $TPL->ContentHead; } ?>
</head>
<body>
<div id="content">
<?php if(isset($TPL->ContentBody)) { include $TPL->ContentBody; } ?>
</div>
</body>
</html>
And finally, add your page with the body content:
最后,在页面中加入正文内容:
<?php
#Hello.php
require_once('lib/PageTemplate.php');
# trick to execute 1st time, but not 2nd so you don't have an inf loop
if (!isset($TPL)) {
$TPL = new PageTemplate();
$TPL->PageTitle = "My Title";
$TPL->ContentBody = __FILE__;
include "layout.php";
exit;
}
?>
<p><?php echo "Hello!"; ?></p>
#3
4
This is a basic approach but, yeah, it does work :) I sure would bother with a lot of templating and OOP but you are definitely on the right path
这是一种基本的方法,但是,是的,它确实有用:)我肯定会为大量的模板和OOP而烦恼,但您肯定走对了路
As i can't comment anymore, then i will answer here ;) If he need a custom title then he needs some more advanced functions. So, as i told, this is a basic approach. But in the end, if he really have a static header/footer, and really use them everywhere, well, yes, this is a good way to go.
因为我不能再评论了,所以我在这里回答;如果他需要一个自定义标题,那么他需要一些更高级的功能。正如我所说,这是一个基本的方法。但是最后,如果他真的有一个静态的页眉/页脚,并且在任何地方都使用它们,那么,是的,这是一个很好的方法。
So ofc you could bother with some advanced headers with parameters you could feed on each page. You could go on a whole MVC stuff. In the end just tell him to use a pre-made framework and stop bothering. How could he learn if you don't let him do some trial and error ?
所以ofc你可以在每个页面上添加一些高级标题和参数。你可以使用整个MVC。最后,告诉他使用一个预先准备好的框架,不要再麻烦了。如果你不让他反复试验,他怎么可能学会呢?
#4
-6
index.php -- includes header, footer, and content based on REQUEST variable.
header.php -- header content
footer.php -- footer content
索引。php——包括基于请求变量的页眉、页脚和内容。头。php——页眉内容页脚。php,页脚内容
content1.php, content2.php, etc.
content1。php,content2。php等。
index.php:
index . php:
<?php
include ('header.php');
// VERY IMPORTANT - do not use the GET variable directly like this
// make sure to filter it through a white-list
include(basename($_GET['page']).'.php');
include ('footer.php');
?>
if you want the URL to go www.domain.com/pagename where the page you're trying to load into index.php is "pagename", use HTACCESS and do some URL Rewriting: http://corz.org/serv/tricks/htaccess2.php
如果您想要将URL转到www.domain.com/pagename,则需要将页面加载到index中。php是“pagename”,使用HTACCESS并做一些URL重写:http://corz.org/serv/phints/htaccess2.php