簡體   English   中英

HTML / PHP:HTML頭的哪些部分可以保存在單獨的文件中

[英]HTML / PHP: Which parts of an HTML head can be saved in a separate file

我是HTML新手,希望有人可以幫助我。

我正在嘗試建立一個網站,其中每個頁面的代碼都保存為一個單獨的php文件。 由於所有這些頁面都使用相同的文檔頭和包含我想從頁面中盡可能多地刪除它並將其保存在單獨的包含文件header.php )中,然后我可以使用PHP包含它。

我當前的頭部看起來如下,因為這在我目前擁有的所有頁面上是相同的,所有這些保存在單獨的header.php文件中,然后只使用<?php include "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">
        <!-- ... -->

這應該給你一般的想法......

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

由於您在header.php中開始標記,您可以在自己的頁面上關閉標記(我的意思是其他頁面,即:聯系人,關於我們,產品等...)。

您在問題中提到的代碼部分可以稱為header.php並且可以進一步包含在需要它的每個頁面上。

除此之外

  1. 標題 - header.php
  2. 主要內容 - main-content.php - 這將特定於您網站的每個頁面
  3. Footer - footer.php - 這也可以是常見的 - 並且還包含在每個頁面中。
  4. 除此之外,您還可以擁有一些可能具有導航或任何類型的常見內容塊的php文件。

示例頁面可能是:

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

你的頭文件(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文件中,當你必須維護網站時,這會大大改善你的生活(想象一下,你可以在你的網站的每個頁面上添加樣式表或庫。一段時間......)

為了幫助SEO,你應該指定一個標題,也許是一些頁面特定的元標記。

還要仔細考慮是否在每個頁面中都有重復的其他部分,例如左側或右側欄或特定小部件(例如橫幅圖像或類似內容)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM