問題描述
我正在尋求有關將站點內容分成邏輯塊的最佳實踐的建議.我想要一個在整個站點中保持不變的頁眉和頁腳,這樣如果我有幾個不同內容的頁面,它們都會如下所示——對頁眉和頁腳所做的更改然后自動更新,而我不必更改每個單獨的頁面.
<身體><p>頁面內容在此</p>
我正在尋求有關將站點內容分成邏輯塊的最佳實踐的建議.我想要一個在整個站點中保持不變的頁眉和頁腳,這樣如果我有幾個不同內容的頁面,它們都會如下所示——對頁眉和頁腳所做的更改然后自動更新,而我不必更改每個單獨的頁面.
<身體><p>頁面內容在此</p>
<?包括'footer.php';?>
header.php
將包含開頭的 、
和靜態內容,以及
footer.php
將包含任何額外的靜態內容和結束 </html>
標簽.所以,我的問題是:這是一個好方法嗎?我擔心將 標簽散布在多個文件中是不好的做法.如果是這樣,處理這種設計的正確方法是什么?
不,你的方法是錯誤的.
以下是您設計中的主要缺陷:
每個人都必須牢記的主要規則:
在所有數據準備就緒之前,不必將任何字符發送到瀏覽器中.
為什么?
HTTP header
.有時我們必須發送它們.如果您已經發送了華麗的 HTML 標頭,這是不可能的.
標簽.這不是很常見的事情嗎?但是如果不使用模板,你就無法做到.因此,您必須擁有一個包含頁眉和頁腳的通用站點模板,以及每個 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:
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?
HTTP header
. Sometimes we have to send them. And it's gets impossible if you already have your ornate HTML header sent.<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 = "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,
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 包含分隔網站內容的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
主站蜘蛛池模板: 美女久久| 亚洲一区二区三区久久 | 岛国午夜| 久久精品久久综合 | 久久久精品影院 | 久久久精品久久久 | 成人免费淫片aa视频免费 | 日韩色在线 | 亚洲成人av在线 | 精品综合久久久 | 中文字幕免费中文 | 精品国产欧美在线 | 日韩欧美1区2区 | 久久久久久久久国产成人免费 | 亚洲国产欧美日韩 | 亚洲国产成人久久综合一区,久久久国产99 | 色av一区二区三区 | 亚洲一区视频在线 | 欧美一区二区免费视频 | 日韩第一区 | 久久久www成人免费精品 | 欧美一区二区免费电影 | 天天干精品 | 亚洲网站在线观看 | 欧美精品一区二区在线观看 | 精品一区二区三区在线观看 | 国产一区二区三区在线 | 欧美精品久久久 | 一区二区中文 | 一级在线观看 | 亚洲视频中文 | 超碰97人人人人人蜜桃 | 精品一二区 | 一区二区免费在线视频 | 草在线 | 国产aⅴ精品 | 精品综合网 | 久久性| 99综合| 亚洲va欧美va天堂v国产综合 | 久久天天躁狠狠躁夜夜躁2014 |