HTML / PHP:HTML头的哪些部分可以保存在单独的文件中

时间:2015-06-13 15:17:38

标签: php html include html-head

我是HTML新手,希望有人能帮助我。

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

我当前的头部看起来如下,因为我在目前拥有的所有页面上都是相同的,所有这些都保存在单独的header.php文件中,然后在单个页面上使用header.php

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

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

我的PHP(标题文件):

<?php include "header.php"; ?>

5 个答案:

答案 0 :(得分:1)

这应该给你一般的想法...

<!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" />

    <!--
    Your paths need to relative to the websites root directory or the full
    URL else they may not work depending on where the file is located.
    -->     
    <link rel="stylesheet" type="text/css" href="/includes/styles.css" />
    <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" />

    <!-- 
    This line just echos the title if one has been set or a default string
    if the title hasn't been set. You will need to ensure every page that
    includes this file sets the title before including this file. You may
    also want to take this approach with other values.
    -->
    <title><?php echo isset($title) ? $title : 'Default Title'; ?></title>
</head>
<body>
    <div class="wrapper">

答案 1 :(得分:1)

由于您在header.php中开始标记,您可以在自己的页面上关闭标记(我的意思是其他页面,即:联系人,关于我们,产品等...)。

答案 2 :(得分:1)

您在问题中提到的代码部分可以称为header.php,并且可以进一步包含在需要它的每个页面上。

除此之外

  1. 标题 - header.php
  2. 主要内容 - main-content.php - 这将特定于您网站的每一页
  3. 页脚 - footer.php - 这也可以是常见的 - 并且还包括在每一页中。
  4. 除此之外,你还可以拥有一些可能有导航或任何类型的常见内容块的php文件。
  5. 示例页面可能是:

    <?php
    include 'header.php'
    /*either add here the content or include further php files*/
    include 'about-us.php'
    include 'footer.php'
    ?>
    

答案 3 :(得分:1)

您的头顶文件(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">
        <!-- ... -->

答案 4 :(得分:1)

你做得对,所有页面之间共享的部分应放在一个单独的php文件中,这大大改善了你必须维护网站时的生活(想象一下,你可以在每个页面上添加样式表或库。一段时间后你的网站......)。

为了帮助SEO,你应该指定一个标题,也许是一些特定于页面的元标记。

还要仔细考虑是否在每个页面中都有重复的其他部分,例如左侧或右侧栏或特定小部件(例如横幅图像或类似内容)