PHP包括每页技术的导航栏? [英] PHP Include Nav-bar For Every Page Techniques?

查看:115
本文介绍了PHP包括每页技术的导航栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过包括文件来整理每个页面的代码来清理我的PHP代码:

I'm trying to clean up my code in my php web pages by including files to consolidate each page's code's like so:

文件:head.php



File: head.php

<!DOCTYPE html>
<html>
<head>
<title> *Dynamic Page Title* </title>
<meta name"description" "Dynamic Description">
<link href="#">
</head>

档案:footer.php

File: footer.php

<footer>
   <ul>
      <a href="#"><li>Home</li></a>
      <a href="#"><li>Page 1</li></a>
      <a href="#"><li>Page 2</li></a>
   </ul>
</footer>
<script src="#">
</body>
</html>

档案:navbar.php

File: navbar.php

<header class="navbar navbar-default navbar-static-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <a href="#" class="navbar-brand"></a>
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></button>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a href="index">Home</a></li>                    
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">FAQ</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Contact Us</a></li>
            </ul>
        </div>
    </div>
</header>

档案:index.php

File: index.php

<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Home has an active class
<?php include('footer.php'); ?>

档案:Page-1.php

File: Page-1.php

<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Page 1 in nav-bar has an active class
<?php include('footer.php'); ?>

你们得到主要的想法。我想了解如何在导航栏中的不同页面中添加活动类,以及如何向每个页面添加不同的标题和元标记。

You guys get the main idea. I would like tips on how to add active classes in different pages in the nav-bar, and how to add different titles and meta tags to each page.

任何帮助?

推荐答案

您可以在文件中定义变量:

You can define variables in your files:

index.php

<?php $title = 'Home'; ?>
<?php $metaTags = 'tag1 tag2'; ?>
<?php $currentPage = 'index'; ?>
<?php include('head.php'); ?>
<?php include('nav-bar.php'); ?>
<body>
Index Page, Home has an active class
<?php include('footer.php'); ?>

然后在包含的文件中使用这些变量:

Then use these variables in the included file:

head.php

<!DOCTYPE html>
<html>
<head>
<title><?php echo($title); ?></title>
<meta name"description" "<?php echo($metaTags); ?>"><!-- note that this code is wrong -->
<link href="#">
</head>

navbar.php

<header class="navbar navbar-default navbar-static-top">
    <div class="container-fluid">
        <div class="navbar-header">
            <a href="#" class="navbar-brand"></a>
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"><i class="fa fa-bars"></i></button>
        </div>
        <div class="navbar-collapse collapse">
            <ul class="nav navbar-nav navbar-right">
                <li <?php if ($currentPage === 'Home') {echo 'class="active"';} ?>><a href="index">Home</a></li>                    
                <li><a href="#">Page 1</a></li>
                <li><a href="#">Page 2</a></li>
                <li><a href="#">FAQ</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Contact Us</a></li>
            </ul>
        </div>
    </div>
</header>

在最后一部分中,使用数组更容易,而不是为每行重复相同的代码:

In this last part, it's easier to use an array instead of repeating the same code for each line:

<ul class="nav navbar-nav navbar-right">
    <?php
        // Define each name associated with an URL
        $urls = array(
            'Home' => '/',
            'Page 1' => '/page1',
            // …
        );

        foreach ($urls as $name => $url) {
            print '<li '.(($currentPage === $name) ? ' class="active" ': '').
                '><a href="'.$url.'">'.$name.'</a></li>';
        }
    ?>
</ul>

这篇关于PHP包括每页技术的导航栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆