Tag Archives: Master Pages

Simple MasterPages with PHP

If you are an ASP.Net developer used to working with MasterPages (or any equivalant on another framework), you may be looking for an equivalant while working with php.  I was looking to do something like that with my homepage (hoolihan.net).  This is a very simple site, where a full framework would have been overkill.

Here’s how I pieced it out:

First: Create a template page, I called mine master.php.

<div><?php include('menu.php');?></div>
<div><?php include($page_content);?></div>
<div><?php include('footer.php');?></div>

Second: Create a content piece, for example about_text.php.
<p>This is information about me.</p>

Third: Create the page with the actual name you want to use:
$page_content = 'about_text.php';

One added benefit is that the content piece is then loadable in other places if need be. 

Obviously, you’ll want to add a few bells and whistles.   For example, It’s a good idea to check for $page_content being null, and provide a default in that case.  I left out some of those things for the sake of brevity.

For more patterns in php, check out the following books:

Basically, it’s a real simple way to cleanly break up your php pages.  Hope you found  this useful…

Update: I’ve posted a follow-up article about the ajax ideas mentioned in this article. Also, see more php posts on this blog and more programming posts.

Update 2: User Ton posted a good comment below about how to setup the content variable (and simplify your url) with apache.