<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="css/style.css"/>
<link rel="stylesheet" type="text/css" href="css/homepage.css"/>
<link rel="stylesheet" type="text/css" href="css/header.css"/>
<link rel="stylesheet" type="text/css" href="css/footer.css"/>
<link rel="stylesheet" type="text/css" href="css/navmenu.css"/>
<!-- more css here -->
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/navmenu.js"></script>
<!-- more js here -->
</head>
我在 <head></head>
中拥有所有外部 css 和 javascript。我的所有页面中的标记,所有这些在所有页面中都很重要。将所有这些内容放在一个单独的文件中并使用 php 包含它是否合适?因此,如果我想对这些外部进行一些更改,这对我来说很容易,因为它将影响我的所有页面..我只是想知道这是否是一个好的做法..提前致谢
最佳答案
是的,确实如此。但为什么要停在那里呢?理想情况下,您应该将所有重复标记放在一个文件中。
在 PHP 中共享通用标记的方法有很多,但最简单的方法是使用全局“Top.php”和“Bottom.php”文件,如下所示:
在 Top.php 中:
<html>
<head>
<title><?php echo $pagetitle; ?></title>
<!-- your <meta /> elements go here -->
</head>
<body>
<!-- common page elements go here -->
在Bottom.php中:
</body>
</html> <!-- This ensures all of the common markup is closed -->
然后对于每个页面,执行以下操作:
<?php $pageTitle = "This page's title";
require("Top.php"); ?>
<!-- put your per-page markup and PHP code here -->
<?php require("Bottom.php"); ?>
简单。
现在我如何使用 require()
而不是 include()
。 require函数比较严格,基本保证包含的文件存在。我认为应用程序明显崩溃比默默失败要好。
关于php - 将头标签信息存储在单独的文件中并稍后包含它是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12320216/