Welcome to WebmasterServe!

FREE TO JOIN! Join us now to engage in informative and friendly discussions about Webmastering, SEO, SEM, Internet Marketing, Programming, Graphic Design, Online Jobs and more. What are you waiting for? Ready to join our friendly community? It takes just one minute to register.

Dismiss Notice

Join WebmasterServe Forums 
Join the discussion! Have a better idea or an opinion? It takes just one minute to register Click Here to Join

Tutorial Simple php template tutorial

Discussion in 'General Programming Discussions' started by theomnis, Mar 29, 2005.

Thread Status:
Not open for further replies.
  1. theomnis

    theomnis Guest

    Ratings:
    +0 / -0
    I am currently using this method on one of my sites, and I thought I would share it with you. If you have an in-depth knowledge of PHP, then this tutorial probably isnt for you, seeing as there are more efficient ways of doing the same thing. However, if you are just getting into PHP or dont know much PHP.

    The following method of using PHP templates is useful if you have alot of pages that use the same design. It makes it so that if you want to make a layout change, or change a menu in your site, you do not have to update numerous HTML files. You can simply edit one PHP file.

    Firstly, you will need a PHP file to use as a template. This will be your layout. Name it "template.php", or something else if you really want to. This is where most of the site design will be done. Simply make a layout, and then where you want the content section of the page to be, put the following PHP code:

    <?php
    include($pagename);
    ?>

    This makes it so that the section of the site you put the code in will be filled with the specified file, which in this case will be an HTML file. Also, make sure that any links to other sections of your site link to a PHP file, like "contact.php" or "downloads.php".

    You can view a sample PHP template file at http://www.gameglyph.com/template.php.

    Ok, now back to those other PHP files. Let's assume that "contact.php" will be your page. In "contact.php" put the following code:

    <?php
    $pagename = "contact.html";
    require_once("template.php");
    ?>

    This will set the variable $pagename to the corresponding HTML file which would be where you actually put the page's content. It will then load "template.php" which will see that you have defined $pagename, and load that HTML file.

    So, in conclusion, each page is made up of three files.

    PHP Template File - "template.php" - This is the general layout and unchanging areas of your site.

    PHP Page File - "contact.php" - This is the file that loads the template, and tells the template which HTML file to put in the designated content section.

    HTML Content File - "contact.html" - The corresponding HTML file that is used as the actual content of the page.

    Again, this is just a basic method. If you are using "index.php" you will either have to name the corresponding HTML file to something like "home.html" or just change your site indexing priorities to make it so that PHP index files come before HTML files. Also, if you want to use a PHP file as the content page, you can just use "contact2.php" or something to that extent.

    A functional example of this method can be seen at http://www.gameglyph.com. If you have any questions or comments, please feel free to reply.
     
  2. John121

    Yellow Belt

    Joined:
    Mar 8, 2005
    Messages:
    147
    Ratings:
    +0 / -0
    Hey theomnis! thanks a billion man.. I needed that. I know very little php and I struggle with it.

    So when you are putting that code it, you just place that strip of code where ever you want that "item" to come up at?
     
  3. Cannonballguy

    Yellow Belt

    Joined:
    Mar 28, 2005
    Messages:
    18
    Ratings:
    +0 / -0
    Hmmm.. Interesting way of doing things.
    Thanks for posting it.
    And John: Yes.
     
  4. John121

    Yellow Belt

    Joined:
    Mar 8, 2005
    Messages:
    147
    Ratings:
    +0 / -0
    Okay so say you wanted to add a block to your site. You would creat the design in save it in an html file... then you'd find where you want to put it then you'd say:
    PHP:
    Select All
    <?php
       
    include('$blocknew')
    ?>
    Is that how it would be?
     
  5. Cannonballguy

    Yellow Belt

    Joined:
    Mar 28, 2005
    Messages:
    18
    Ratings:
    +0 / -0
    Im pretty new to PHP, but yeah, that looks right.
     
  6. John121

    Yellow Belt

    Joined:
    Mar 8, 2005
    Messages:
    147
    Ratings:
    +0 / -0
    Okay.. I need a lil more help. So I would put this:
    PHP:
    Select All
    <?php (
       
    $blocknew=blocknew.html); 
    blablablablablablabla include
            (
    '$blocknew'); 
    ?>
     
  7. theomnis

    theomnis Guest

    Ratings:
    +0 / -0
    That looks like it should work. Are you having any problems with it?
     
  8. John121

    Yellow Belt

    Joined:
    Mar 8, 2005
    Messages:
    147
    Ratings:
    +0 / -0
    Nah its workin

    Thanks pal :)
     
  9. MadGangsta

    Yellow Belt

    Joined:
    Mar 28, 2005
    Messages:
    154
    Ratings:
    +0 / -0
    Lol, there goes John always figuring stuff out
     
  10. fiverivers

    fiverivers Guest

    Ratings:
    +0 / -0
    Thanks alot for putting this message really very informatics I know very little php and I struggle with it. So please also update the code it.
     
Loading...
Thread Status:
Not open for further replies.

Share This Page