php - HTML/PHP : Which parts of an HTML head can be saved in a separate file

标签 php html include html-head

我是 HTML 新手,希望有人能帮助我解决这个问题。

我正在尝试建立一个网站,其中每个页面的代码都保存为单独的 php 文件。 由于所有这些页面都使用相同的文档头并包含我想从页面中删除尽可能多的内容并将其保存在单独的包含文件中(header.php)然后我可以使用 PHP 包含它。

我当前的头部如下所示,由于所有页面上的内容都是相同的,所以我目前将所有这些内容保存在单独的 header.php 文件中,然后仅在单个页面上使用 <?php include "header.php"; ?>

有人可以告诉我哪些部分应该保留在单个页面上以进行正确的设置以及是否应该在此处更改或添加任何内容?

注意: jQuery 和 JS 分别包含在页脚中。

我的 PHP(头文件):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="author" content="John Doe" />
        <meta name="description" content="Created: 2015-06" />

        <base href="http://www.MyURL.com" target="_self" />

        <!-- CSS -->        
        <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <!-- CSS - Font Awesome -->
        <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

        <title>My Page</title>
    </head>

    <body>
        <div class="wrapper">
        <!-- ... -->

最佳答案

您的顶级头文件 (top_head.php):

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <?php if(isset($page_author)){ ?>
            <meta name="author" content="<?php echo $page_author; ?>" />
        <?php } ?>

        <?php if(isset($page_description)){ ?>
            <meta name="description" content="Created: 2015-06" />
        <?php } ?>

        <!-- CSS -->        
        <link rel="stylesheet" type="text/css" href="includes/styles.css" />
        <!-- CSS - Font Awesome -->
        <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

        <title><?php echo $page_title; ?></title>

您当前的头文件:

<?php
      $page_title = 'Hello';
      $page_description = 'This is a beautiful description';
?>
<?php include("top_head.php"); ?>
</head>

    <body>
        <div class="wrapper">
        <!-- ... -->

关于php - HTML/PHP : Which parts of an HTML head can be saved in a separate file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30820266/

相关文章:

php - Docker Compose 与 PHP、MySQL、nginx 连接问题

javascript - 限制复制表格的数量

c - cc(文件名).c -l(库名)-o(输出文件名)有什么作用?为什么它可以工作,而 make 却不能?

具有 GET 属性的 PHP include() (include file.php?q=1)

php - 如何在 PHP cURL 中提供 Youtube 身份验证 token

javascript - 如何使用 JSON 编码数据定义具有 sql 行 id 的元素

php - 用户消息系统(PHP/MySQL)

javascript - 用js选择随机表格单元格

javascript - 显示密码计强度级别

c++ - 删除不必要的包含时的最佳实践是什么?