Another disclaimer: I take no credit for this invention. I found it while googling and will provide the link at the end of the post.
I work in a .NET environment and have been introduced to the concept of master pages and controls along with the standard aspx page. Even though .NET can be maddening, master pages are pretty great for use in centralizing your design. When design changes need to be made (style, structural, whatever) you literally fix one file and it displays sitewide.
As I was redoing my site structure I wondered if I could use this same concept but with PHP instead. After some searching I found a post that showed how to do it at http://spinningtheweb.blogspot.com/2006/07/approximating-master-pages-in-php.html. The sample code given was a little cumbersome (if you read my aversion to tables in the last post), but the idea was really simple and effective. So I set up some test pages and sure enough it worked great. I have implemented this concept sitewide, but with some necessary changes.
These master pages work really well… until you start going down into your directory structure. You can reference lower level files to the higher level master page, but it can cause issues.
Here is a sample master page based on my code (stripped down of course):
<html>
<head>
<title>kevware.com: <?php echo $pagetitle; ?></title>
<link href=”css/pagestyle.css” rel=”stylesheet” type=”text/css” />
</head>
<body>
<div class=”pagecontainer”>
<?php include(“nav.php”); ?>
<div class=”maincontentdiv”>
<div class=”include”><?php echo $pagemaincontent; ?></div>
</div>
</div>
<?php include(“footer.php”); ?>
</body>
</html>
Here is a sample page using the master page above:
<?php
//Buffer larger content areas like the main page content
ob_start();
?>
<p>The page you are looking for doesn’t exist anymore, or never did
in the first place… sorry!Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. Fusce ac quam. Donec neque. Mauris et eros eget erat dapibus mollis. Mauris laoreet posuere odio. Nam ipsum ligula, ullamcorper eu, fringilla at, lacinia ut, augue. Nullam nunc.</p>
<?php
//Assign all Page Specific variables
$pagemaincontent = ob_get_contents();
ob_end_clean();
$pagetitle = “Sample Page Title”;
//Apply the template
include(“master.php”);
?>
I use includes for the navigation and the footer. I also referenced what I hoped would be a master css file. The problem arises when you have a subdirectory page referencing the higher level master. It seems to get a bit confused with all of the moving parts. It finds the master okay, but looks for the includes and css file on its’ own level. So this example only works for a subdirectory (where the sample page references ../master.php) if that particular master references ../nav.php and ../footer.php.
There is still a problem with the style sheets though. If search engines retrieved old pages that were two or even three levels down it would invoke my 404 error page on that level, and the styles would reference from THAT level. And since the master page references the style sheet on its’ own directory level at css/pagestyle.css, this cause much of the page to not show and look pretty janky.
To work with the subdirectory page the link would have to be ../css/pagestyle.css. And that seemed a little nonsensical to me. So in order to make things clean and orderly (because I like clean and orderly) I decided to create a master page that would be used on each level, tucked away in a directory that also had an accompanying css folder and style sheet. Files on the root level reference that master page (and css). Files on subdirectory 1 level reference subdirectory 1 master page… and so on. I also put a nav and footer file on each of those levels so that it’s about as clean as it can get, and nothing is referencing files not on its’ own level. I only have 3 levels, so it’s not a pain thank god.
So all in all not as clean as a .NET master page, but still makes a world of difference in simplifying overall site code on individual pages. Let me know if something isn’t clear and I’ll do my best to make it clearer.