使用 PHP 包含分隔网站内容

2021-12-24 00:00:00 layout php

我正在寻求有关将站点内容分成逻辑块的最佳实践的建议.我想要一个在整个站点中保持不变的页眉和页脚,这样如果我有几个不同内容的页面,它们都会如下所示——对页眉和页脚所做的更改然后自动更新,而我不必更改每个单独的页面.

<身体><p>页面内容在此</p><?包括'footer.php';?>

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

解决方案

不,你的方法是错误的.
以下是您设计中的主要缺陷:

  1. 您假设在每次页面调用时都会调用 header.php.错了.
  2. 您假设 header.php 将始终是静态的.那是错误的.
  3. 您忘记为页面本身创建模板.

每个人都必须牢记的主要规则:

在所有数据准备就绪之前,不必将任何字符发送到浏览器中.

为什么?

  • 今天是 2011 年.阿贾克斯时代.如果您的代码必须发送 JSON 数据而不是整个 HTML 页面怎么办?
  • 有一种东西叫做HTTP header.有时我们必须发送它们.如果您已经发送了华丽的 HTML 标头,这是不可能的.
  • 它仅适用于 4 页的网站.好的.想象一下,您很幸运,收到了另一个 4 页站点的请求.您将只需要更改模板,而不要更改引擎文件.这真是一个巨大的好处.
  • 假设您要根据页面内容为您的页面制作一个自定义的 </code> 标签.这不是很常见的事情吗?但是如果不使用模板,你就无法做到.</li></ul><p>因此,您必须拥有一个包含页眉和页脚的通用站点模板,以及每个 php 脚本的专用模板.</p><p>一个示例布局将是这样的:</p><p>.1.页面本身.</p><p>它什么都不输出,但只收集所需的数据并调用模板:</p><pre><code><?php//包括我们的设置,连接到数据库等.包括目录名($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';//获取需要的数据$DATA=dbgetarr("SELECT * FROM links");$pagetitle = "朋友网站的链接";//等等//然后调用一个模板:$tpl = "links.tpl.php";包括template.php";?></code></pre><p>.2.<code>template.php</code> 这是您的主要站点模板,</p><p>由页眉和页脚组成:</p><pre><code><html xmlns="http://www.w3.org/1999/xhtml"><头><title>我的网站.<?=$pagetitle?></title></头部><身体><div id="页面"><?php 包含 $tpl ?><p></body></html></code></pre><p>.3.最后 <code>links.tpl.php</code> 是实际的页面模板:</p><pre><code><h2><?=$pagetitle?></h2><ul><?php foreach($DATA as $row): ?><li><a href="<?=$row['link']?>"target="_blank"><?=$row['name']?></a></li><?php endforeach ?><ul></code></pre><p>简单、干净且易于维护.</p><p>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.</p><pre><code><?php include 'header.php'; ?> <body> <p>page content here</p> </body> <? include 'footer.php'; ?> </code></pre><p>The <code>header.php</code> would contain the opening <code><html></code>, <code><head></code> and static content, and the <code>footer.php</code> would contain any extra static content and the closing <code></html></code> tag. So, my question is: Is this a good approach? I'm worried that spreading the <code><html></code> tags across multiple files is bad practice. If so, what is the right way to approach this kind of design?</p> 解决方案 <p>Nope, your approach is wrong.<br> Here are main faults in your design:</p><ol> <li>You're assuming that header.php would be called on the every page call. That's wrong.</li> <li>You're assuming that header.php will always be static. That's wrong. </li> <li>You forgot to create a template for the page itself.</li> </ol><p>The main rule everyone have to learn by heart:</p> <p>Not a single character has to be sent into browser, until all data gets ready.</p> <p>Why?</p> <ul> <li>it's 2011 today. AJAX era. What if your code will have to send JSONed data instead of whole HTML page? </li> <li>there is a thing called <code>HTTP header</code>. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.</li> <li>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.</li> <li>Imagine you're going to make a custom <code><title></code> 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> </ul> <p>So, you have to have one common site template containing header and footer and also dedicated templates for the every php script.</p> <p>An example layout is going to be like this:</p> <p>.1. page itself. </p> <p>it outputs nothing but only gather required data and calls a template:</p><pre><code><?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"; ?> </code></pre><p>.2. <code>template.php</code> which is your main site template, </p> <p>consists of your header and footer:</p><pre><code><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> </code></pre><p>.3. and finally <code>links.tpl.php</code> is the actual page template:</p><pre><code><h2><?=$pagetitle?></h2> <ul> <?php foreach($DATA as $row): ?> <li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li> <?php endforeach ?> <ul> </code></pre><p>easy, clean and maintainable.</p> </div> <div class=""> <p><strong>相关文章</strong></p> </div> </article> </div> </main> <footer> <div class="container"> <p> <span>友情链接:</span> <a href="https://www.688576.com" target="_blank">雨伦博客</a>   <a href="https://www.yaanbbs.net" target="_blank">雅安论坛</a> </p> <a href="https://beian.miit.gov.cn" target="_blank">京ICP备15023317号-6</a> </div> </footer> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?30b42218aa13759c43de5f1971d0a93b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>