PHP 中 HTML 头的最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8247001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:16:14  来源:igfitidea点击:

Best practice for HTML head in PHP

phphtml

提问by Joseph Duffy

I currently have a very simple page, with some more complicated backend. I've always done this the same way, but I feel it's not right. But I haven't come up with much useful to me.

我目前有一个非常简单的页面,有一些更复杂的后端。我一直都是这样做的,但是我觉得不对。但是我没有想出对我有用的东西。

I've got my index.php, header.php and function.php. index.php includes header.php, which calls a function from function.php. The main thing I'm not sure about is how to make the website dynamic enough that it can have easily editable pages, but also be easily editable is a big part of it needs editing.
index.php

我有我的 index.php、header.php 和 function.php。index.php 包括 header.php,它从 function.php 调用一个函数。我不确定的主要事情是如何使网站具有足够的动态性以使其具有易于编辑的页面,而且易于编辑是其中很大一部分需要编辑。
索引.php

<?php
session_start();
include_once 'header.php';
?>
// Page content
</body>
</html>

header.php

头文件

<!DOCTYPE HTML>
<html lan="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<?php
require_once 'functions.php';
getBar(); // Top of page bar with menu/login etc
?>

I don't think showing functions.php would add that much. I know I shouldn't use 2 <head>s in my file, so what do I do if I want to have a description on each page? I've previously had the files setup like: index.php

我不认为显示functions.php 会增加那么多。我知道我不应该<head>在我的文件中使用 2 s,那么如果我想在每个页面上都有一个描述怎么办?我以前有过这样的文件设置:index.php

<?php
session_start();
include_once 'header.php';
?>
// extra page specific stuff here, such as description or JS
</head>
<body>
<?php
require_once 'functions.php';
getBar(); // Top of page bar with menu/login etc
?>
// Page content here
</body>
</html>

header.php

头文件

<!DOCTYPE HTML>
<html lan="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="style.css" type="text/css" />

However, I think the files are "fragmented". I don't know if there is a generally accepted way of doing this, but if there is, I'd love to hear it!

但是,我认为这些文件是“碎片化的”。我不知道是否有一种普遍接受的方式来做到这一点,但如果有,我很想听听!

采纳答案by dqhendricks

I think most experienced coders would tell you to mix your code and your HTML as little as possible. This means doing all of your calculations, then simply passing any variables needed by the view to the view.

我认为大多数有经验的程序员会告诉你尽可能少地混合你的代码和你的 HTML。这意味着完成所有计算,然后简单地将视图所需的任何变量传递给视图。

They would also tell you to look into the MVC design pattern. In PHP, this is where Controller classes take the initial request, perform the correct operations on the correct business Model classes & then pass needed data to a View class, which would render your HTML.

他们还会告诉您研究 MVC 设计模式。在 PHP 中,这是 Controller 类接收初始请求的地方,对正确的业务模型类执行正确的操作,然后将所需的数据传递给 View 类,该类将呈现您的 HTML。

MVC can go from very simple 3 class variations, to extremely complex "Frameworks" like Zend Framework. If you build your own, look into PHP class autoloading, as it will make your life easier.

MVC 可以从非常简单的 3 个类变体到极其复杂的“框架”,如 Zend 框架。如果您构建自己的,请查看 PHP 类自动加载,因为它会让您的生活更轻松。

回答by Your Common Sense

Methinks that you are looking for templates.

我认为您正在寻找模板。

Though you almost nailed it, you are mixing matters a little. Just separate your "design parts" from "code parts". Don't make header to do the job of the actual code and everything become as clear as a fresh water

虽然你几乎搞定了,但你有点混淆了。只需将“设计部分”与“代码部分”分开即可。不要让标题来完成实际代码的工作,一切都变得像淡水一样清澈

Let me suggest you the following setup:
A site contains of

让我建议您进行以下设置:
一个站点包含

  • main config file (suppose it's something like your functions.php)
  • main site template
  • actual pages (or sections) - actual php scripts of different purpose. Each consists of 2 parts:
    • script itself, doing all data mining and preparations, initialising all variables
    • a template, doing the job of data outputonly.
  • 主配置文件(假设它类似于你的functions.php)
  • 主站点模板
  • 实际页面(或部分) - 不同用途的实际 php 脚本。每个由两部分组成:
    • 脚本本身,进行所有数据挖掘和准备工作,初始化所有变量
    • 一个模板,只做数据输出的工作。

the main idea is to make PHP script do no output until all data got ready - thus you will get an instant solution of your page title problem. Moreover (think about it):

主要思想是让 PHP 脚本在所有数据准备就绪之前不输出 - 因此您将获得页面标题问题的即时解决方案。此外(想一想):

  • you will be able to send whatever HTTP headers, cookies, start session etc.
  • you will be also able to change whole output appearance on the fly: an XML or JSON instead of HTML for example
  • you will be able to use the same code base for the different sites, changing only templates
  • 您将能够发送任何 HTTP 标头、cookie、启动会话等。
  • 您还可以即时更改整个输出外观:例如 XML 或 JSON 而不是 HTML
  • 您将能够为不同的站点使用相同的代码库,只更改模板

Once php script done with data preparations, it calls for the the main site timplate, which, in turn, will include the actual page template.

一旦 php 脚本完成了数据准备,它就会调用主站点模板,而该模板又将包含实际的页面模板。

Thus you will get exactly what you're asking for - the website dynamic enough that it can have easily editable pages, but also be easily editable is a big part of it needs editing - and many more other benefits.

因此,您将得到您想要的东西——网站足够动态,它可以拥有易于编辑的页面,而且易于编辑是它需要编辑的很大一部分——以及更多其他好处。

Template I am talking about is a typical PHP script, however, consists mostly of pure HTML, with some PHP code only to display dynamically generated data.

我所说的模板是一个典型的 PHP 脚本,但是,它主要由纯 HTML 组成,有一些 PHP 代码仅用于显示动态生成的数据。

Here you go with some basic yet working example

在这里,您可以使用一些基本但有效的示例

the page:

这一页:

<?
//include our settings, connect to database etc.
include dirname($_SERVER['DOCUMENT_ROOT']).'/cfg/settings.php';
//getting required data
$DATA=dbgetarr("SELECT * FROM links");
// setting title for using in the main template
$pagetitle = "Links to friend sites";
//etc
//set page template filename
$tpl = "links.tpl.php";
//and then finally call a template:
include "main.tpl.php";
?>

where main.tpl.phpis your main site template, including common parts, like header, footer, menu etc:

main.tpl.php您的主站点模板在哪里,包括常见部分,如页眉、页脚、菜单等:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>My site. <?=$pagetitle?></title>
</head>
<body>
<div id="page">
<? include $tpl ?>
</div>
</body>
</html>

and links.tpl.phpis the actual page template:

并且links.tpl.php是实际的页面模板:

<h2><?=$pagetitle?></h2>
<ul>
<? foreach($DATA as $row): ?>
<li><a href="<?=$row['link']?>" target="_blank"><?=$row['name']?></a></li>
<? endforeach ?>
<ul>

回答by Madara's Ghost

The general accepted approach in small sitesis to have something like this:

小型站点中普遍接受的方法是这样的:

<?php $title = 'Title'; ?>
<?php require_once('header.php'); ?>

Header.php

头文件

<title><?php echo $title; ?> - MyWebsite</title>

I think that should work fine for you, while still preserving the K.I.S.S principle.

我认为这应该对你有用,同时仍然保留 KISS 原则。

回答by Aerik

What I have done is make header and footer functions that take an array as a parameter, and the array may contain optional elements, so you would pass in the title, etc. to the header function.

我所做的是制作以数组为参数的页眉和页脚函数,并且该数组可能包含可选元素,因此您可以将标题等传递给 header 函数。

I think one thing you will find that is "generally accepted" is to use PHP more and more as a program, and less to drop out of html when you need dynamic content only. In other words, writing everything as a program that outputs html, not as a mix of html and php (opening and closing the PHP tags).

我认为您会发现“普遍接受”的一件事是越来越多地将 PHP 用作程序,而当您只需要动态内容时,更少地退出 html。换句话说,将所有内容编写为输出 html 的程序,而不是 html 和 php 的混合(打开和关闭 PHP 标记)。

In this model, many websites only have one "index.php" and that actually generates all the pages (often from a database). But if you've only got handful of pages, then each being it's own php page with shared header and footer functions is a fine way to go, IMHO.

在这个模型中,许多网站只有一个“index.php”,它实际上生成了所有页面(通常来自数据库)。但是,如果您只有少数页面,那么每个页面都是自己的带有共享页眉和页脚功能的 php 页面,这是一个不错的方法,恕我直言。