When you’re creating a website with lots of pages all using the same look and feel, headers, CSS etc, it obviously makes sense to create a simple template.
Begin by creating a page in XHTML/HTML that looks and works exactly as you wish. Now cut and paste the header into a new file and put it into a folder called /includes/ on your server. This should be saved as something like header.php. Don’t worry that it’s not actually PHP code, that doesn’t matter. Do the same thing for your main navigation (the navigation that will remain the same throughout). You could put that in header.php but I like to keep it separate just in case there are rare cases where the navigation isn’t needed on a particular page. If you have subnavigation that changes section by section isolate that and put it in its own php file and finally, do the same thing for your footer.
You should now have an HTML file with just the content in it. Assuming you’ve used the same file names and structure as me, you’ll need to add the following code to the top of your HTML pages:
<?php
require($_SERVER['DOCUMENT_ROOT'].”/includes/header.php”);
require($_SERVER['DOCUMENT_ROOT'].”/includes/subnav_home.php”);
require($_SERVER['DOCUMENT_ROOT'].”/includes/navigation.php”);
?>
This approach ensures that, whatever the structure of your site, your script can find the php/HTML files it needs to include.
Right at the end of your page add these lines:
<?php
require($_SERVER['DOCUMENT_ROOT'].”/includes/footer.php”);
?>
You should rename the file so that it has a .php extension rather than .html so that your server will process it as PHP.
Test it to see that it still looks the same as the original file and, if it works, you can now create new web pages by simply adding the indicated lines at the beginning and end and then just writing content using standard XHTML between them. The big advantage is that, when you change your site navigation or anything else that appears in those included files, you only have to do it once.


.gif)