Tag Archives: PHP

More on Using Masterpages (Layouts) Patterns in Languages Like PHP

In a prior post, I wrote about using php to implement a simple version of layouts, or what are called masterpages in ASP.Net. In it I mentioned that you could go directly to the content part for ajax purposes. I want to show how to support that in a language like php when not using a framework.

I’m going to try to get back to posting more about solutions that apply to simpler sites. I think rightfully so, developers have been backing off the complexity of large frameworks for everyday web projects. Often times, with just a few lightweight tools, clean maintainable web apps can be created. Listen to this episode of hanselminutes: it happens to be about WebMatrix, but could be an advertisement for php, sinatra and more.

Let’s say that I wanted to link to an about page, and using the convention from the prior post, I have masterpage.php, about.php, and about_text.php. Any link that goes to about should look like the following:

<a href="about.php" 
      alt="About Page" 

So if the user has javascript disabled, they will be sent to about.php where they will see the full layout and content. But if the javascript works, then it will popup just the content in a modal dialogue. Note that you have to implement the loadContent function yourself. For an example, see the following jqModal post.

Finally, it’s worth knowing that on the ruby side (for example using sinatra), you can control whether you use a layout on the server side with the following code:

if request.xhr?
  erb :about, :layout => false
  erb :about

For more ideas, check out books like Real-World Solutions for Developing High-Quality PHP Frameworks and Applications, or discuss below if you have ideas for more posts about solutions for your next web project. Also, checkout other php posts and programming posts.

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.