Advanced Search

Results 1 to 2 of 2

Thread: Making PHP Templates

  1. #1
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Making PHP Templates

    Hi there,

    I'm here to show you today how to make PHP templates! PHP templates are much easier to manage in real world applications, and are extremely easy to make. Let's begin.

    Estimated time: 10 minutes if you're starting fresh, and about roughly 5 minutes per page if you're converting.
    Skills you need: Very basic PHP knowledge, some HTML, and the ability to delete code.

    We'll start off with the basic URL structure.

    Right now, I'll assume that you have something like this:

    http://www.mydomain.com/mypage.php
    http://www.mydomain.com/anotherpage.php

    After this tutorial, you'll end up with something like this:

    http://www.mydomain.com/index.php?p=mypage
    http://www.mydomain.com/index.php?p=anotherpage

    So, firstly, strip out all your html code EXCEPT for the code between the <body></body> tags in all your pages except the index page (or where you want your "master page" to be) and put all your files (again, except the master page) in one folder. We'll use "inc" as an example.

    And then... place this code where you want your content to be.

    PHP Code:
    <?php
    $default 
    'home'//Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.
    $page = isset($_GET['p']) ? $_GET['p'] : $default//Checks if ?p is set, and puts the page in and if not, it goes to the default page.
    $page basename($page); //Gets the page name only, and no directories.
    if (!file_exists('inc/'.$page.'.php'))    { //Checks if the file doesn't exist
        
    $page $default//If it doesn't, it'll revert back to the default page
        //NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.
    }
    include(
    'inc/'.$page.'.php'); //And now it's on your page!
    ?>
    Alrighty, we've now got the hard part done.

    Now, if you're converting your pages into this template format, you'll need to modify your menu links. Trust me, it's worth it!

    Say you have your menu code like this,
    HTML Code:
    <ul>
      <li><a href="mypage.php">My Page</a></li>
      <li><a href="someotherpage.php">Some Other Page</a></li>
    </ul>
    You'll need to change it to this:
    HTML Code:
    <ul>
      <li><a href="?p=mypage">My Page</a></li>
      <li><a href="?p=someotherpage">Some Other Page</a></li>
    </ul>
    ...in order for it to work.

    Now, if you want to get a little bit more complex, you can make a PHP function for it to generate the menu for you!

    That way, you can add menu items easily and even change the window title!

    You'll need to make some modifications to the original PHP code in order for this to work.

    Firstly, replace your whole menu code with this:
    PHP Code:
    <?php generateMenu(); ?>
    And somewhere on your page, or on another page place this:
    (Make sure that it is BEFORE your content grabber snippet (The first PHP code on this tutorial)
    PHP Code:
    <?php
    $menu 
    = array();
    $menu['home'] = 'Home';
    $menu['mypage'] = 'My Page';
    //Add in the format of: $menu['page name'] = 'Page Title';
    $title='Home'//Default title
    function generateMenu()    {
        global 
    $menu,$default,$title;
        echo 
    '    <ul>';
        
    $p = isset($_GET['p']) ? $_GET['p'] : $default;
        foreach (
    $menu as $link=>$item)    {
            
    $class='';
            if (
    $link==$p)    {
                
    $class=' class="selected"';
                
    $title=$item;
            }
            echo 
    '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>';
        }
        echo 
    '</ul>';
    }
    ?>
    Change accordingly.

    And on your document title, place:
    PHP Code:
    <?php echo $title?>
    Wherever you want the title of the page to be.

    Here's an example of the title:
    HTML Code:
    <title>My PHP Template - <?php echo $title; ?></title>
    Ok, that's the end of this tutorial. If you have any questions, feel free to start a thread on it.

    For those who don't have time to read this whole thing, here's the whole PHP code:

    PHP Code:
    <?php
    $menu 
    = array();
    $menu['home'] = 'Home';
    $menu['mypage'] = 'My Page';
    //Add in the format of: $menu['page name'] = 'Page Title';
    $title='Home'//Default title
    function generateMenu()    {
        global 
    $menu,$default,$title;
        echo 
    '    <ul>';
        
    $p = isset($_GET['p']) ? $_GET['p'] : $default;
        foreach (
    $menu as $link=>$item)    {
            
    $class='';
            if (
    $link==$p)    {
                
    $class=' class="selected"';
                
    $title=$item;
            }
            echo 
    '<li><a href="?p='.$link.'"'.$class.'>'.$item.'</a></li>';
        }
        echo 
    '</ul>';
    }
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>My PHP Template - <?php echo $title?></title>
    </head>

    <body>
    <?php
    generateMenu
    ();
    $default 'home'//Whatever default page you want to display if the file doesn't exist or you've just arrived to the home page.
    $page = isset($_GET['p']) ? $_GET['p'] : $default//Checks if ?p is set, and puts the page in and if not, it goes to the default page.
    if (!file_exists('inc/'.$page.'.php'))    { //Checks if the file doesn't exist
        
    $page $default//If it doesn't, it'll revert back to the default page
        //NOTE: Alternatively, you can make up a 404 page, and replace $default with whatever the page name is. Make sure it's still in the inc/ directory.
    }
    include(
    'inc/'.$page.'.php'); //And now it's on your page!
    ?>
    </body>
    </html>
    Edit: Added basename($page) for extra security
    Last edited by tech_support; 01-09-2008 at 08:44 AM.
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This is a very basic way of doing templates. It may work for small applications, but it's not as powerful as a real full-sized templating engine such as Smarty.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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