简体   繁体   English

在我的页面中添加 head 部分的 php 包含是一个好习惯吗?

[英]Is it good practice to add a php include of the head section in my pages?

I am creating my portfolio site and I am wanting to include the head section as a php include on my page.我正在创建我的投资组合网站,我想将 head 部分作为 php 包含在我的页面上。 Reason being is because the site will have a fair few pages and I will want to make changes later on to things later on like tidying up the css files.原因是因为该站点将有相当多的页面,我想稍后对诸如整理 css 文件之类的事情进行更改。

For example;例如;

<head>
    <?php include('head.php'); ?>
</head>

as opposed to all this below being shown on each and every page:与下面的所有这些都显示在每一页上相反:

    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title></title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">

    <link rel="stylesheet" href="css/normalize.css">
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="css/1140.css">
    <link rel="stylesheet" href="css/ie.css">
    <script src="js/vendor/modernizr-2.6.1.min.js"></script>
</head>

I just didn't know if this was good practice to do, as with this being my portfolio site, I need the code to be correct from the start also as they will probably look into the standard of it also.我只是不知道这是否是一个好的做法,因为这是我的投资组合网站,我也需要代码从一开始就是正确的,因为他们可能也会研究它的标准。

What are your opinions and advice people?你有什么意见和建议? Thanks.谢谢。

Yep, it's quite standard.是的,这很标准。 But instead of writing:但不是写:

<head>
    <?php include('head.php'); ?>
</head>

you should put the tags inside head.php .你应该把标签放在head.php里面。 I say it's better because what's inside head.php has no sense without the head tags, so they are kinda linked together.我说它更好,因为head.php里面的head.php没有 head 标签就没有意义,所以它们有点联系在一起。 It's good practice to join things so linked into a single file without having to repeat open and close head tags for each page.将如此链接的内容连接到单个文件中是一种很好的做法,而不必为每个页面重复打开和关闭head标签。

Actually, it's even good practice (and commonly used) to have header.php , body.php and footer.php files that has respectively:实际上,让header.phpbody.phpfooter.php文件分别具有:

header.php头文件

<html>
<head>
...
</head>
<body>

body.php正文.php

...

footer.php页脚.php

</body>
</html>

I'm doing that in my application but I've found that it's not a good idea, because you have many of your stylesheets, javascripts, etc in a php file including the head section and you'll have problems with including it in php files in nested folders.我正在我的应用程序中这样做,但我发现这不是一个好主意,因为您在包含 head 部分的 php 文件中有许多样式表、javascripts 等,并且将其包含在 php 中时会遇到问题嵌套文件夹中的文件。 this problem is because of relative paths.这个问题是因为相对路径。 If you can use absolute paths then it's ok otherwise it's not a good idea ...如果您可以使用绝对路径,那就可以了,否则这不是一个好主意......

PHP Includes are used like this all the time. PHP Includes 一直都是这样使用的。 Any time that you have content that will be the exact same on every page, it is very helpful to use an include任何时候当您的每个页面上的内容都完全相同时,使用 include 会非常有帮助

This is an old topic but I use这是一个老话题,但我使用

    <?php include_once("phpinclude/head.txt"); ?>

phpinclude is it's own folder and I keep the footer, header, and common place info in that folder. phpinclude 是它自己的文件夹,我将页脚、页眉和公共位置信息保存在该文件夹中。 .js, and .css has it's own as well. .js 和 .css 也有它自己的。

Edit: I use require now.编辑:我现在使用要求。 I would rather have a code fail and die rather than give some random string.我宁愿让代码失败并死亡,也不愿给出一些随机字符串。 They are the same except one dies and the other will print out an error or random code.它们是相同的,除了一个死了,另一个会打印出错误或随机代码。 This is for people learning PHP, not old heads.这是给学习 PHP 的人,而不是老头子的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM