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

查看:26
本文介绍了使用 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>它<strong>什么都不输出</strong>,但只收集所需的数据并调用模板:</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 ?></div><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: <em>Is this a good approach?</em> 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><div class="h2_lin"> 解决方案 </div><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><strong>Not a single character has to be sent into browser, until all data gets ready.</strong></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 <strong>nothing</strong> 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> <p>这篇关于使用 PHP 包含分隔网站内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!</p> </div> <div class="arc-body-main-more"> <span onclick="unlockarc('2714501');">查看全文</span> </div> </div> <div> </div> <div class="wwads-cn wwads-horizontal" data-id="166" style="max-width:100%;border: 4px solid #666;"></div> </div> </article> <div id="arc-ad-2" class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="widget bgwhite radius-1 mb-1 shadow widget-rel"> <h5>相关文章</h5> <ul> <li> <a target="_blank" title="使用PHP包含来分隔网站内容" href="/1546452.html"> 使用PHP包含来分隔网站内容; </a> </li> <li> <a target="_blank" title="从网站内容PHP过滤JavaScript" href="/870991.html"> 从网站内容PHP过滤JavaScript; </a> </li> <li> <a target="_blank" title="PHP:如何根据 Javascript 抓取网站内容" href="/2680187.html"> PHP:如何根据 Javascript 抓取网站内容; </a> </li> <li> <a target="_blank" title="网站内容无法隐藏" href="/1371215.html"> 网站内容无法隐藏; </a> </li> <li> <a target="_blank" title="网站内容搜索问题" href="/1230976.html"> 网站内容搜索问题; </a> </li> <li> <a target="_blank" title="Django存储网站内容" href="/2049332.html"> Django存储网站内容; </a> </li> <li> <a target="_blank" title="扫描网站内容(快速)" href="/2899581.html"> 扫描网站内容(快速); </a> </li> <li> <a target="_blank" title="如何使用iframe显示网站内容" href="/1393414.html"> 如何使用iframe显示网站内容; </a> </li> <li> <a target="_blank" title="PHP:如何抓取基于Javascript的网站内容" href="/588582.html"> PHP:如何抓取基于Javascript的网站内容; </a> </li> <li> <a target="_blank" title="如何使用 php 高效翻译网站内容,保持 SEO 友好?" href="/2445299.html"> 如何使用 php 高效翻译网站内容,保持 SEO 友好?; </a> </li> <li> <a target="_blank" title="如何阅读网站内容?" href="/2125017.html"> 如何阅读网站内容?; </a> </li> <li> <a target="_blank" title="PHP流下载网站内容,直到找到字符串" href="/654997.html"> PHP流下载网站内容,直到找到字符串; </a> </li> <li> <a target="_blank" title="使用安全登录来刮除网站内容" href="/588768.html"> 使用安全登录来刮除网站内容; </a> </li> <li> <a target="_blank" title="如何使用批处理脚本读取网站内容?" href="/1966407.html"> 如何使用批处理脚本读取网站内容?; </a> </li> <li> <a target="_blank" title="网站导航显示网站内容,例如列表名称" href="/1358747.html"> 网站导航显示网站内容,例如列表名称; </a> </li> <li> <a target="_blank" title="有关抓取网站内容的建议" href="/997853.html"> 有关抓取网站内容的建议; </a> </li> <li> <a target="_blank" title="为什么我无法使用CURL获取网站内容" href="/590012.html"> 为什么我无法使用CURL获取网站内容; </a> </li> <li> <a target="_blank" title="如何更改网站内容使用Chrome扩展程序? (例)" href="/821761.html"> 如何更改网站内容使用Chrome扩展程序? (例); </a> </li> <li> <a target="_blank" title="如何从需要cookie登录的网站中抓取PHP中的网站内容?" href="/548297.html"> 如何从需要cookie登录的网站中抓取PHP中的网站内容?; </a> </li> <li> <a target="_blank" title="如何从需要 cookie 登录的网站中抓取 PHP 中的网站内容?" href="/2680056.html"> 如何从需要 cookie 登录的网站中抓取 PHP 中的网站内容?; </a> </li> <li> <a target="_blank" title="将网站内容读入字符串" href="/911835.html"> 将网站内容读入字符串; </a> </li> <li> <a target="_blank" title="将网站内容加载到属性中" href="/1493490.html"> 将网站内容加载到属性中; </a> </li> <li> <a target="_blank" title="如何使用 .net 桌面应用程序复制网站内容" href="/2491018.html"> 如何使用 .net 桌面应用程序复制网站内容; </a> </li> <li> <a target="_blank" title="如何使用Powershell/Jquery添加“查看所有网站内容"链接" href="/1375309.html"> 如何使用Powershell/Jquery添加“查看所有网站内容"链接; </a> </li> <li> <a target="_blank" title="从左侧视图隐藏网站内容" href="/1359710.html"> 从左侧视图隐藏网站内容; </a> </li> </ul> </div> <div class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="side"> <div class="widget widget-side bgwhite mb-1 shadow"> <h5>PHP最新文章</h5> <ul> <li> <a target="_blank" title="请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers" href="/558143.html"> 请求头字段Access-Control-Allow-Headers在预检响应中不允许Access-Control-Allow-Headers; </a> </li> <li> <a target="_blank" title="路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误" href="/548154.html"> 路由问题导致Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException错误; </a> </li> <li> <a target="_blank" title="什么是NCFB和NOFB模式?" href="/681571.html"> 什么是NCFB和NOFB模式?; </a> </li> <li> <a target="_blank" title="警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是)" href="/594668.html"> 警告:mysqli_connect():(HY000 / 1045):访问被拒绝用户'用户名'@'localhost'(使用密码:是); </a> </li> <li> <a target="_blank" title="如何处理致命错误:cURL错误7:无法连接到xxxx端口443" href="/589688.html"> 如何处理致命错误:cURL错误7:无法连接到xxxx端口443; </a> </li> <li> <a target="_blank" title="参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串" href="/748988.html"> 参数3传递给GuzzleHttp\Client :: request()必须是数组类型,给定字符串; </a> </li> <li> <a target="_blank" title="phpMyAdmin的#2054无法登录到MySQL服务器" href="/218086.html"> phpMyAdmin的#2054无法登录到MySQL服务器; </a> </li> <li> <a target="_blank" title="SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败" href="/215284.html"> SSL错误SSL3_GET_SERVER_CERTIFICATE:证书验证失败; </a> </li> <li> <a target="_blank" title="在PHPExcel中设置字体颜色,字体和字体大小" href="/511258.html"> 在PHPExcel中设置字体颜色,字体和字体大小; </a> </li> <li> <a target="_blank" title="如何解决cURL错误(7):无法连接到主机?" href="/588378.html"> 如何解决cURL错误(7):无法连接到主机?; </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门教程 </h5> <ul> <li> <a target="_blank" title="Java教程" href="/OnLineTutorial/java/index.html"> Java教程 </a> </li> <li> <a target="_blank" title="Apache ANT 教程" href="/OnLineTutorial/ant/index.html"> Apache ANT 教程 </a> </li> <li> <a target="_blank" title="Kali Linux教程" href="/OnLineTutorial/kali_linux/index.html"> Kali Linux教程 </a> </li> <li> <a target="_blank" title="JavaScript教程" href="/OnLineTutorial/javascript/index.html"> JavaScript教程 </a> </li> <li> <a target="_blank" title="JavaFx教程" href="/OnLineTutorial/javafx/index.html"> JavaFx教程 </a> </li> <li> <a target="_blank" title="MFC 教程" href="/OnLineTutorial/mfc/index.html"> MFC 教程 </a> </li> <li> <a target="_blank" title="Apache HTTP客户端教程" href="/OnLineTutorial/apache_httpclient/index.html"> Apache HTTP客户端教程 </a> </li> <li> <a target="_blank" title="Microsoft Visio 教程" href="/OnLineTutorial/microsoft_visio/index.html"> Microsoft Visio 教程 </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门工具 </h5> <ul> <li> <a target="_blank" title="Java 在线工具" href="/Onlinetools/details/4"> Java 在线工具 </a> </li> <li> <a target="_blank" title="C(GCC) 在线工具" href="/Onlinetools/details/6"> C(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="PHP 在线工具" href="/Onlinetools/details/8"> PHP 在线工具 </a> </li> <li> <a target="_blank" title="C# 在线工具" href="/Onlinetools/details/1"> C# 在线工具 </a> </li> <li> <a target="_blank" title="Python 在线工具" href="/Onlinetools/details/5"> Python 在线工具 </a> </li> <li> <a target="_blank" title="MySQL 在线工具" href="/Onlinetools/Dbdetails/33"> MySQL 在线工具 </a> </li> <li> <a target="_blank" title="VB.NET 在线工具" href="/Onlinetools/details/2"> VB.NET 在线工具 </a> </li> <li> <a target="_blank" title="Lua 在线工具" href="/Onlinetools/details/14"> Lua 在线工具 </a> </li> <li> <a target="_blank" title="Oracle 在线工具" href="/Onlinetools/Dbdetails/35"> Oracle 在线工具 </a> </li> <li> <a target="_blank" title="C++(GCC) 在线工具" href="/Onlinetools/details/7"> C++(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="Go 在线工具" href="/Onlinetools/details/20"> Go 在线工具 </a> </li> <li> <a target="_blank" title="Fortran 在线工具" href="/Onlinetools/details/45"> Fortran 在线工具 </a> </li> </ul> </div> </div> </div> <script type="text/javascript">var eskeys = '使用,php,包含,分隔,网站,内容'; var cat = 'cc';';//php</script> </div> <div id="pop" onclick="pophide();"> <div id="pop_body" onclick="event.stopPropagation();"> <h6 class="flex flex101"> 登录 <span onclick="pophide();">关闭</span> </h6> <div class="pd-1"> <div class="wxtip center"> <span>扫码关注<em>1秒</em>登录</span> </div> <div class="center"> <img id="qr" src="https://huajiakeji.com/Content/Images/qrydx.jpg" alt="" style="width:150px;height:150px;" /> </div> <div style="margin-top:10px;display:flex;justify-content: center;"> <input type="text" placeholder="输入验证码" id="txtcode" autocomplete="off" /> <input id="btngo" type="button" onclick="chk()" value="GO" /> </div> <div class="center" style="margin: 4px; font-size: .8rem; color: #f60;"> 发送“验证码”获取 <em style="padding: 0 .5rem;">|</em> <span style="color: #01a05c;">15天全站免登陆</span> </div> <div id="chkinfo" class="tip"></div> </div> </div> </div> <script type="text/javascript" src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/highlight.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/base.js?v=0.22"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/tui.js?v=0.11"></script> <footer class="footer"> <div class="container"> <div class="flink mb-1"> 友情链接: <a href="https://www.it1352.com/" target="_blank">IT屋</a> <a href="https://huajiakeji.com/" target="_blank">Chrome插件</a> <a href="https://www.cnplugins.com/" target="_blank">谷歌浏览器插件</a> </div> <section class="copyright-section"> <a href="https://www.it1352.com" title="IT屋-程序员软件开发技术分享社区">IT屋</a> ©2016-2022 <a href="http://www.beian.miit.gov.cn/" target="_blank">琼ICP备2021000895号-1</a> <a href="/sitemap.html" target="_blank" title="站点地图">站点地图</a> <a href="/Home/Tags" target="_blank" title="站点标签">站点标签</a> <a target="_blank" alt="sitemap" href="/sitemap.xml">SiteMap</a> <a href="/1155981.html" title="IT屋-免责申明"><免责申明></a> 本站内容来源互联网,如果侵犯您的权益请联系我们删除. </section> <!--统计代码--> <script type="text/javascript"> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?0c3a090f7b3c4ad458ac1296cb5cc779"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript"> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </div> </footer> </body> </html>