我是 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/