在PHP的所有页面上包含标题

时间:2016-05-08 15:24:16

标签: javascript php html

我正在建立一个网站,我正在使用PHP。我创建了一个名为header.php的文件(包含需要包含在我所有页面中的标题)。

让我使用我的文件header.php的内容来更好地解释自己。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8"/>
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		
		<title>Fashion</title>
		
		<link rel="stylesheet" href="css/header.css"/>		
		<link rel="stylesheet" href="css/slider.css"/>
		<link rel="stylesheet" href="css/bloc-page.css"/> <!--Load it when the user is on the page clothes.php-->
		<link rel="stylesheet" href="css/footer.css"/>
		<link rel="stylesheet" href="css/header-search.css"/>
		<link rel="stylesheet" href="css/back-to-top.css"/>
		<link rel="stylesheet" href="css/form-register.css"> <!--Load it when the user is on the page register.php-->
		<link rel="stylesheet" href="css/form-login.css">
		<link rel="stylesheet" type="text/css" href="css/style-carousel.css" />  <!--Load it when the user is on shoes.php-->
		<link rel="stylesheet" type="text/css" href="css/owl.carousel.css" />
		<link rel="stylesheet" type="text/css" href="css/owl.theme.default.min.css" />
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script type="text/javascript" src="js/form-register.js"></script>
		<script src="js/modernizr.js"></script> <!-- Modernizr -->
	</head>

我面临的问题是,当我在某些页面上时,我想加载某些CSS个文件。例如,我想在用户bloc-page.css

时加载文件clothes.php

我正在考虑使用正则表达式,但我想知道是否有更好的方法。

感谢您的回复

2 个答案:

答案 0 :(得分:2)

在包含header.php之前,你可以创建一个包含所有nessecary addon-css的数组

<?php
$addonCss = Array("bloc-page.css");
include "header.php";
//... rest of page
?>

然后检查header.php中是否有这样的数组:

<?php
//....
if(isset($addonCss)) {
     for($i=0;$i<count($addonCss); $i++) {
         echo "<link rel=\"stylesheet\" href=\"css/".$addonCss[$i]."\"/>\n";
     }
}
//...
?>

这只是一种(在这里最简单)有很多种可能性。

答案 1 :(得分:0)

制作基本网址并将其全部插入src,例如

$base_url="mydomain.com";
<link rel="stylesheet" href="<?php echo $base_url ?>css/header.css"/>   
相关问题