Using PHP include to separate site content(使用 PHP 包含分隔网站内容)
问题描述
我正在寻求有关将站点内容分成逻辑块的最佳实践的建议.我想要一个在整个站点中保持不变的页眉和页脚,这样如果我有几个不同内容的页面,它们都会如下所示——对页眉和页脚所做的更改然后自动更新,而我不必更改每个单独的页面.
<身体><p>页面内容在此</p><?包括'footer.php';?>
header.php
将包含开头的 、
和静态内容,以及
footer.php
将包含任何额外的静态内容和结束 </html>
标签.所以,我的问题是:这是一个好方法吗?我担心将 标签散布在多个文件中是不好的做法.如果是这样,处理这种设计的正确方法是什么?
不,你的方法是错误的.
以下是您设计中的主要缺陷:
- 您假设在每次页面调用时都会调用 header.php.错了.
- 您假设 header.php 将始终是静态的.那是错误的.
- 您忘记为页面本身创建模板.
每个人都必须牢记的主要规则:
在所有数据准备就绪之前,不必将任何字符发送到浏览器中.
为什么?
- 今天是 2011 年.阿贾克斯时代.如果您的代码必须发送 JSON 数据而不是整个 HTML 页面怎么办?
- 有一种东西叫做
HTTP header
.有时我们必须发送它们.如果您已经发送了华丽的 HTML 标头,这是不可能的. - 它仅适用于 4 页的网站.好的.想象一下,您很幸运,收到了另一个 4 页站点的请求.您将只需要更改模板,而不要更改引擎文件.这真是一个巨大的好处.
- 假设您要根据页面内容为您的页面制作一个自定义的
标签.这不是很常见的事情吗?但是如果不使用模板,你就无法做到.
因此,您必须拥有一个包含页眉和页脚的通用站点模板,以及每个 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:
- You're assuming that header.php would be called on the every page call. That's wrong.
- You're assuming that header.php will always be static. That's wrong.
- 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?
- 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.
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 = "TGlua3MgdG8gZnJpZW5kIHNpdGVz";
//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.
这篇关于使用 PHP 包含分隔网站内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 包含分隔网站内容
- PHP Count 布尔数组中真值的数量 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- Laravel 仓库 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01