SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to create templates?

    I want to create templates for my website, so in the future when I want to change my design, I don't have to modify 100's of PHP files separatley. Can someone lead me to the right track?

  2. #2
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a very simple example.

    a) The template file. An HTML with some php varibles in it whe will want to replace with text.

    Code:
    <html>
    <head>
    <title>$title</title>
    <body bgcolor="#EBE9E3">
    <p>$main</p>
    </body>
    b) The php file that does the work.

    PHP Code:
    // define some variables

    $title 'Template test' ;
    $main 'HelloWorld' ;

    // grab the template file

    ob_start() ;
    include( 
    'templates/main.html' ) ;
    $pageHTML addslashesob_get_contents() );
    ob_end_clean() ;

    // parse the template to add the vars

    eval( "\$pageHTML = \"$pageHTML\";" ) ;

    // send to the browser

    echo $pageHTML 
    Hope that gets you started.

    Matt.

  3. #3
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't really see what advantages that way has to
    PHP Code:
    include "header.php";
    Content goes here
    include "footer.php"
    with dynamic code in the header file to set titles etc. based on filenames.

  4. #4
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Minsor;

    I understood all, except

    PHP Code:
    // grab the template file

    ob_start() ;
    include( 
    'templates/main.html' ) ;
    $pageHTML addslashesob_get_contents() );
    ob_end_clean() ;

    // parse the template to add the vars

    eval( "\$pageHTML = \"$pageHTML\";" ) ;

    // send to the browser

    echo $pageHTML 
    I dont get the point of those? What do they do to the *.htm file you gave? :\

  5. #5
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agree with anode - for all of my sites I have :
    PHP Code:
    include ("header.php") ;

    echo 
    "hello" ;

    include (
    "footer.php") ; 
    Then you just edit the header or footer file and your done
    Luke
    Luke-Martin.com (work in progress) :: Independent Web Designer's Portal
    Freelance Forums :: Sign Up Here
    "What happens if you get scared half to death twice?"

  6. #6
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by anode
    I don't really see what advantages that way has to
    PHP Code:
    include "header.php";
    Content goes here
    include "footer.php"
    It really depends on what you're doing.

    Can you imagine vBulletin using includes for the templating? *shudder*

    One plus for templating in this way is error capture. Imagine if you will that you are creting a dynamic form for user completion. Say that two thirds of the way down the page you create a drop down box populated from your database, what happens if the query to get the select items fails? If you just supress the errors, you get the form with the default select items, but this isn't complete and will most probably confuse the user. On the other hand, you could just kill the script at the point the query fails. But what if the item you were rendering was going to be just off the bottom of the screen for a user viewing at 800x600. they're going to fill in half the form, and then scroll down to find there is no more form. Very annoying. If you buffer your output in this way, you can very easily fork out of you main page on errors and simply show/log errors instead of part-pages.

    Another thing would be that templates that are full pages/tables/forms are much easier to load and understand than part pages. If you have part of a table in one template, and part in another, it's all to easy to mess up the html by editing one part and not the other. These things are sometimes easy to remember when you first build a system, but for someone else editing the templates at a later date, simplicity is a massive time and error saving advantage.

    Anyway, that's just two simple reasons to mull over.

    Matt.

  7. #7
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mincer's method looks somethign new for me hehe, care to explain how each line work?

  8. #8
    SitePoint Wizard Mincer's Avatar
    Join Date
    Mar 2001
    Location
    London | UK
    Posts
    1,140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For Gamerz and kvk007, an explanation of how the code works

    PHP Code:
    // grab the template file

    ob_start() ;
    include( 
    'templates/main.html' ) ;
    $pageHTML addslashesob_get_contents() );
    ob_end_clean() ; 
    ok, this bit is actually a lot simpler than it looks. All the functions that start ob_ relate to output buffering.

    The first line 'ob_start()' turns on output buffering. This captures anything that the script would normally just send strgight to the browser in a buffer.

    The second line grabs the template page via an include. But as explained above, instead of being send straight to the browser, it is captured in the buffer.

    The third line grabs the contents of the output buffer (ob_get_contents()), escapes any characters in the file that would break a php string (addslahes()), and dumps it to a variable.

    The fourth line closes and empties the output buffer.

    PHP Code:
    // parse the template to add the vars

    eval( "\$pageHTML = \"$pageHTML\";" ) ; 
    eval() evaluates a string as if it were php code. This means the variables in our temaplte get replaced by their values that we assigned earlier.

    PHP Code:
    // send to the browser

    echo $pageHTML 
    I think that bit if now pretty obvious.

    There are various other ways of grabbing the contents of the template file before we parse it, but the output buffer way is one of the easiest, most reliable, and fastest. There is a new function called file_get_conents(), but it's only in cvs at the moment. This looks to be a very useful function for the future.

    Matt.

  9. #9
    SitePoint Enthusiast kvk007's Avatar
    Join Date
    Sep 2002
    Location
    United States of America
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mincer, thanks for your help! But I guess I am TOO DUMB to understan what happened, even with your help :\

  10. #10
    SitePoint Enthusiast Geoff W's Avatar
    Join Date
    Dec 2002
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In regards to assigning the page to a variable, I would say this method is more efficient than using the output buffer...

    PHP Code:
    $filename "templates/main.html";
    $fd fopen ($filename"r");
    $pageHTML fread ($fdfilesize ($filename));
    $pageHTML addslashes($pageHTML);
    fclose ($fd); 

  11. #11
    SitePoint Guru GamerZ's Avatar
    Join Date
    Nov 2001
    Location
    Singapore
    Posts
    617
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mincer for the explantion, I think I get it

  12. #12
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Mincer


    It really depends on what you're doing.

    Exactly. The evaluate/parse way is overkill for simple content separation like the original poster was asking. For more complicated situations than that, sure it's better.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jul 2003
    Location
    Switzerland
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Geoff W
    In regards to assigning the page to a variable, I would say this method is more efficient than using the output buffer...

    PHP Code:
    $filename "templates/main.html";
    $fd fopen ($filename"r");
    $pageHTML fread ($fdfilesize ($filename));
    $pageHTML addslashes($pageHTML);
    fclose ($fd); 
    Yes is about 4-6 x faster but file_get_contents (PHP > 4.3.0) is also faster then your code. ( I benchmarked it with a tiny and huge file )


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
  •