Results 1 to 31 of 31

Thread: Master Pages in PHP

Threaded View

  1. #16
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Master Pages in PHP

    uh. no, you don't need to use ajax. I think you're confusing ajax with something else. ajax is for dynamically updating things without refreshing a page; what you're describing has absolutely no reason to use ajax.

    a "master page" in ASP.NET lets you simply create a template file that has a content placeholder for where your content will go. then, you assign an ASP file this template and the content in this ASP file appears in the content place holder. with PHP, it's much less structured in that you need to make this system yourself -- this is because PHP is just a language (as is ASP), but ASP.NET is ASP using the .NET framework. just as well, there are many frameworks written in PHP that could help you do the same thing easily, but I won't get into those.

    so, if you want to take the code I wrote in my last post:

    PHP Code:
    <?php
      
    //path to include files
      
    $path '../includes/';

      
    //pages array
      
    $pages = array(124 => 'home.php'2432 => 'about.php'404 => 'error.php');

      
    //get the current request
      
    $page = (isset($_GET['p'])) ? (isset($pages[$_GET['p']]) && file_exists($path $pages[$_GET['p']]) ? $_GET['p'] : 404) : key($pages);

      
    /* this ternary sequence does the following:
       * if $_GET['p'] is set:
       *   - checks if it's a valid value within the $pages array and if that file exists
       *   - sets $page to 404 if the page is invalid or the file doesn't exist
       * elseif $_GET['p'] is not set:
       *   - sets the default page to the first key in the array (in this case, 124)
       */

    ?>
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <h1>title</h1>
        <div id="left">
          <ul>
            <li>navigation</li>
          </ul>
        </div>
        <div id="right">
    <?php

      
    require($pages[$page]);

    ?>
        </div>
        <div id="footer"></div>
      </body>
    </html>
    if you look at how I've structured the HTML in this example, you'll see that this is incredibly similar to ASP's "master pages" with just a little extra. at the top of the script you have the code that handles a request, and then you start your template. in the middle of your template, you have a "content placeholder" where you use require() to include the content file (in this case, $pages[$page] -- which would be equal to "home.php" if $page was 124).

    again, this has nothing to do with ajax. unlike ASP.NET, PHP does not give you a ton of unnecessary mark-up (including javascript) to get basic functionality. PHP lets you build everything yourself. javascript (or ajax) is used specifically for user interaction to make a better browsing experience; this is not something used for "basic functionality."
    Last edited by kows; Jan 26th, 2010 at 10:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width