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>