php模板页眉和页脚

5 html php php-include

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

<?php include_once("header.php")?>
   contents of the page...        
<?php include_once("footer.php")?>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

footer.php 包含:

    <footer>...</footer>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

上面的代码是针对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>
Run Code Online (Sandbox Code Playgroud)

She*_*ose 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")?>
Run Code Online (Sandbox Code Playgroud)

并在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>
Run Code Online (Sandbox Code Playgroud)