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.


Posted

in

, ,

by

Tags:

Comments

21 responses to “Simple MasterPages with PHP”

  1. php Avatar

    much confusion ….. explain in Very Detail manner………..

  2. Vladimir Avatar

    This is great.
    Thanks a lot,
    Vladimir

  3. Nitin Avatar

    interesting..

  4. EC Avatar

    Interesting way of doing this, sure will help, thank you

  5. garzanti Avatar
    garzanti

    Simple and efficient thnx!

  6. Vijay Avatar
    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?

  7. Tim Avatar
    Tim

    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.

  8. Mark V. Avatar

    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?

  9. Tim Avatar
    Tim

    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.

  10. BH Avatar
    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.

  11. Farzad Avatar
    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

  12. Jules Avatar
    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.

  13. Andy Avatar
    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!

    1. Tim Avatar

      Andy – can you post your masterpage?

  14. Tim Avatar
    Tim

    FYI – For help with basic php and patterns, I tended to use PHP and MySQL Web Development (4th Edition)

  15. jignesh Avatar
    jignesh

    need in brief. and with more ex.

  16. Ton Avatar
    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.

  17. Bazinga Avatar
    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.

  18. Peter V Avatar

    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.

  19. Tim Avatar
    Tim

    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

  20. Peter V Avatar

    Hello Tim, I neglected to thank you for your response. I thought I’d wait till I got the site up and running and then show you but then I was distracted and forgot. Anyway, this system worked well for me: http://runforshelter10k.com/