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.

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




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:
<?php
$page_content = 'about_text.php';
include('master.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.

21 thoughts on “Simple MasterPages with PHP

  1. Vijay

    Thanks for the script!

    I am facing a problem with this approach.
    I want to handle some server side script (POST) on a button click in a php file included. Can I achieve this without specifying the method and action in tag of the master php file?

  2. Tim Post author

    i would put your entire form in the content part, not the master. you don’t want to split a form amongst multiple files if possible.

  3. Mark V.

    Hi, I’m an Asp.Net developer. I’d like your post. this will help me on my php projects. Is your code capable of Google Analytics. like in Asp.Net i put the google analytics code in just one template file. how about your code?

  4. Tim Post author

    It works, I use this scheme and google analytics to create my own homepage. Using the example above, I would either put your google analytics code directly into the footer, or into it’s own file and include it from the footer.

  5. BH

    I’m developer of .NET and i use master pages, is most clean and prolix. This post is great!.
    i’m new in php and i have a question, how i can do interchange between the initial content and the assigned to a boton?,i searched in the web and i found that with using $POST i can retrieve signals of events for example: a click. Also in the $POST i change the value of the variable of the content to capture the click event.
    Now the problem is that i can’t do the page refresh with the changes applied.

  6. Farzad

    Hello!
    I’m fairly new in the PHP world, I wonder if you can describe a bit more about your example! Take care and i hope to hear from you!
    Yours sincerely,
    Farzad

  7. Jules

    this might be the best approach i’ve seen so far. only difference with .net masterpages is that if you have 2 placeholders for dynamic content inside your masterpage, you still need to create 2 php-files. In .net you would only need one extra file.

  8. Andy

    Hi I have tried your example and get some strange results.
    I have a link to a css file in my header on the master page. When it renders the page I always get a gap at the top of my page, when I check the html in firebug I could see the link to the css had been moved to the body and lots of extra space had been added at the beginning of the body.

    Any ideas how to fix this, this is a great example and would save me lots of time if I can get it working!

  9. Ton

    Inspired by your approach, I think I’ve come up with an even simpler one. I created the following rewrite url script i .htaccess:

    RewriteEngine on
    RewriteRule ^[/]?$ /home
    RewriteRule ^([A-Za-z]+)/$ /$1 [R]
    RewriteRule ^([A-Za-z]+)$ /master.php?page=$1

    Then I added the following code to master.php:

    Now you can access all your pages like:

    http://yourdomain
    http://yourdomain/home
    http://yourdomain/about

    And the content all resides in a separate /content/ directory that has home.php, about.php etc. The RegEx enforces that you use simple names for the pages, ie., just A-Z or a-z, so code injection is impossible. As a bonus, your visitors cannot see the underlying technology of your site.

  10. Bazinga

    Nice Ton.. I was wondering how to do that. I don’t really know PHP, but I was about to start a site using PHP so that I could learn.. and I was wanting to know how to do that as well.

  11. Peter V

    With respect to Ton’s comments:

    1. Can someone clarify the redirect statements? I think they mean:
    (a) if the page name is not specified (just somedomain.com, with or without a trailing slash), then go to /home
    (b) if the page name is specified with a trailing slash, go to the same page name without a trailing slash
    (c) if the page name is specified, go to /master.php?page=

    2. What should go after
    “Then I added the following code to master.php:”?
    It appears some code was supposed to be inserted but I’m guessing the comments module stripped it out. I think this should go at the top of the file:
    $page_content = $_GET[“page”];

    The url rewriting doesn’t seem to be working on my WAMP server even after enabling Apache > Apache modules > rewrite_module and restarting Apache so I can’t be sure I’m right about #2.

    Otherwise, this has been a helpful post and comments.

  12. Tim Post author

    1)
    #redirect either “” or “/” to “/home”
    RewriteRule ^[/]?$ /home
    #redirect “foo/” to “foo” where foo is all letters
    RewriteRule ^([A-Za-z]+)/$ /$1 [R]
    #redirect “foo” to “/master.php?page=foo” where foo is all letters
    RewriteRule ^([A-Za-z]+)$ /master.php?page=$1

    2)
    I think your correct, he means something like $page_content = $_GET[“page”];

    As for rewrite not working, that can be a lot of things. Google, stackoverflow for answers, etc

Comments are closed.