My friend was asking over on his blog on how to get his navigation setup to show the same on every page so that when he makes one change to it all of the pages will be updated. This is something that I have just done here on this site, you probably didn’t even notice, but I am working behind the scenes to make my site have less data redundancy as well as higher data reuse. Below I’ll describe how I set it up and some code samples I used to make it happen.
I’ll start with my index.php file for my portfolio. Keep in mind that all of these functions can be called anywhere on the site, but the idea is to have one set of code of the header, footer, and navigation sections of the site.
index.php
print_header(“Portfolio”);
?>
<div id=”content”>
<div id=”portfolio”>
** CONTENT GOES HERE **
</div>
</div>
<?php print_footer($PHP_SELF); ?>
So, above I include a global.php file that will control all of the basic site content management.
The reason that I have the include in this file is so that I can include them on other pages all over the entire site. Now, this was originally just my blog, but I am expanding what it on my site, so I am constantly updating this structure. So, really right now I have two set of includes that get used. Some which reside on their own directory, out side of /blog/ and the 1 that is included above. Eventually these will all be on one set of includes and I will just make function calls to generate my pages.
global.php
Below are the current set of functions in this file and what they do.
- print_header($page_name) – This will print the header information for each page and will also give it a title like lazyi.net – $page_name, where page name is like “Portfolio”.
- print_navigation() – This will generate the top navigation on the site.
- print_footer($page) – This will generate the sidebar navigation and print the footer information as well. It takes a page parameter so that you can have the option to print a different side navigation on each page, if you wish.
- sidebar_content($path) – Again, much like above, but this is what builds the side nav.
- print_footerInfo() – Prints the bottom footer on the page
That’s pretty much it. I know it is not the prettiest thing in the world, but it works and it does what I want it to. The beauty of PHP is that you can have it do just about anything and it will work in your favor. If you have questions about this, post it in a comment and I’ll try to address it. Keep in mind that I don’t claim this to be the best implementation of this idea, but it is what I am working on at the moment. So, take from it what you want.
Being the nice guy I am, my sources are below: