使用PHP包含来分隔网站内容 [英] Using PHP include to separate site content

查看:74
本文介绍了使用PHP包含来分隔网站内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找有关将网站内容分成逻辑块的最佳实践的建议.我希望页眉和页脚在整个站点中保持不变,因此,如果我有几页内容不同的页面,它们的外观都将如下所示-对页眉和页脚所做的更改随后会自动更新,而无需我更改每个页面.

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';
?>

header.php将包含开头<html><head>和静态内容,而footer.php将包含任何额外的静态内容和结束</html>标记.因此,我的问题是:这是一种好方法吗?我担心将<html>标记分布在多个文件中是一种不好的做法.如果是这样,进行这种设计的正确方法是什么?

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:

  1. 您假设header.php将在每个页面调用中被调用.错了.
  2. 您假设header.php始终是静态的.错了
  3. 您忘记为页面本身创建模板.

每个人都必须认真学习的主要规则:

The main rule everyone have to learn by heart:

在所有数据准备就绪之前,无需将单个字符发送到浏览器.

为什么?

  • 今天是2011年. AJAX时代.如果您的代码必须发送JSONed数据而不是整个HTML页面怎么办?
  • 有一个叫做HTTP header的东西.有时我们必须发送给他们.如果您已经发送了华丽的HTML标头,那就不可能了.
  • 仅适用于4页网站.好的.想象一下,您很幸运,并请求另一个4页的网站.您将只需要更改模板,而不必触摸引擎文件.那真的是很大的好处.
  • 想象一下,您将根据页面内容为页面创建自定义的<title>标记.这不是很平常的事吗?但是,如果不使用模板,您将无法做到.
  • it's 2011 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page?
  • 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.
  • 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.
  • 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.

因此,您必须有一个公共站点模板,其中包含页眉和页脚以及每个php脚本的专用模板.

So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.

布局示例如下:

.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这是您的主要网站模板,

.2. template.php which is your main site template,

由页眉和页脚组成:

<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.最后links.tpl.php是实际的页面模板:

.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>

简单,干净和可维护.

这篇关于使用PHP包含来分隔网站内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆