php模板页眉和页脚

时间:2014-02-19 06:00:17

标签: php html php-include

对于网站上的每个页面,我都有一个页眉和页脚,所以我决定为两者创建一个模板,然后将它们包含在每个页面上。

<?php include_once("header.php")?>
   contents of the page...        
<?php include_once("footer.php")?>

header.php包含:

<!DOCTYPE html>
<head>
<title>Items Page</title>
<link rel="stylesheet" href="assets/css/item.css">
<script src="assets/css/item.js"></script>
</head>
<body>
    <header>
        contains a navigation bar  
    </header>

footer.php包含:

    <footer>...</footer>
</body>
</html>

上面的代码是针对Items页面的,但我也有Users页面,它们都有相同的页脚和标题标记,但不同的链接CSS和JS文件。如果用户导航到用户页面,如何在<link>包含文件的<script>标记内更改<head>header.php,例如:mysite.com/user/blabla

“用户”页面的正确链接应为:

<link rel="stylesheet" href="assets/css/user.css">
<script src="assets/css/user.js"></script>

6 个答案:

答案 0 :(得分:0)

试试这种方式

用户类型页面

<?php
$pageType = 2; // USER
?>
<?php include_once("header.php")?>
   contents of the page...        
<?php include_once("footer.php")?>

在其他页面

<?php
$pageType = 1; // OTHER
?>
<?php include_once("header.php")?>
   contents of the page...        
<?php include_once("footer.php")?>

并在标题

<!DOCTYPE html>
<head>

<?php if ( $pageType == 1 ) { ?>
    <title>Items Page</title>
    <link rel="stylesheet" href="assets/css/item.css">
    <script src="assets/css/item.js"></script>
<?php } else if ( $pageType == 2 ) { ?>
    <title>User Page</title>
    <link rel="stylesheet" href="assets/css/user.css">
    <script src="assets/css/user.js"></script>
<?php } ?>
</head>

答案 1 :(得分:0)

拆分你的导航和标题包括。

的header.php

<!DOCTYPE html>
<head>
<title>Items Page</title>
<link rel="stylesheet" href="assets/css/item.css">
<script src="assets/css/item.js"></script>

nav.php

</head>
<body>
<header>
    contains a navigation bar  
</header>

whatever.php

<?php include_once("header.php")?>
<link rel="stylesheet" href="assets/css/user.css">
<script src="assets/css/user.js"></script>
<?php include_once("nav.php")?>

答案 2 :(得分:0)

session_start();
if($_SESSION['user'])
{
?>
 <link rel="stylesheet" href="assets/css/item_user.css">
    <script src="assets/css/item_user.js"></script>
<?php
}
else
{
?>
<link rel="stylesheet" href="assets/css/item.css">
    <script src="assets/css/item.js"></script>
<?php
}
?>

答案 3 :(得分:0)

尝试将每个页面设置为css和javascript的变量,如下所示:

 $css        = 'assets/css/user.css';
 $javascript = 'assets/css/user.js';
<?php include_once("header.php")?>
   contents of the page...        
<?php include_once("footer.php")?>
标题页上的

您需要调用这些变量:

<!DOCTYPE html>
<head>
<title>Items Page</title>
<link rel="stylesheet" href="<?php echo $css; ?>">
<script src="<?php echo $javascript; ?>"></script>
</head>
<body>
    <header>
        contains a navigation bar  
    </header>

答案 4 :(得分:0)

在所有页面中使用此格式

<?php    
    $page_title = "title_here";
    $cssfiles   = array("path/to/css1.css", "path/to/css2.css", ...);
    $jsfiles    = array("path/to/js1.js", "path/to/js2.js", ...);    
?>
<?php include_once("header.php")?>
   contents of the page...        
<?php include_once("footer.php")?>

header.php

<head>
    <title><?php echo $page_title;?></title>
    <?php
    foreach($cssfiles as $css){
    ?>
        <link rel="stylesheet" href="<?php echo $css;?>">
    <?php
    }
    foreach($jsfiles as $js){
    ?>
        <script src="<?php echo $js;?>"></script>
    <?php
    }
    ?>
</head>

答案 5 :(得分:0)

你可以这样做

的header.php

if(!isset($custom_css)) {
    echo '<link rel="stylesheet" href="assets/css/item.css">';
}

if(!isset($custom_js)) {
    echo '<script src="assets/css/item.js"></script>';
}

在您的用户文件中,在包含标头之前,为$custom_css$custom_js提供值 user.php的

$custom_css = '<link rel="stylesheet" href="assets/css/user.css">';
$custom_js = '<script src="assets/css/item.js"></script>';
<?php include_once("header.php")?>