Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 5 123 ... LastLast
Results 1 to 15 of 73
  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    General question about best PHP practices.

    There's something that's been driving me crazy about PHP, and for some reason I just can't seem to wrap my mind about it...

    When using PHP includes, which of these is better practice:

    1. Having things such as a DOCTYPE and title in each PHP file, using multiple includes for common parts of a site, like the header, navigation, footer, etc. In other words dealing with things "as they come".

    2. Gathering all your variables up and "passing" them to a single include. I suppose this would be something similar to "templating" like Smarty does.


    I'm prepared for a "there's no one set way to do things" answer, but what's been sending my head around in circles is this: Using method number 2 would essentially mean that you would have to "echo" everything, correct? It seems like it might potentially be a headache to have to get everything into variables first. HOWEVER, number 1 somehow seems "wasteful" in that it would require more include calls (how much do these really bog things down?), and things like your DOCTYPE would be unnecessarily repeated.

    Can someone just tell me which way they prefer and why? The more I think about it the farther away I seem to get from an answer...

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I just make one page then use my own template system to transfer the template to all the other pages. I only really use includes for like db settings, global config settings, functions etc. For html I leave that for the template system

  • #3
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Nightfire View Post
    I just make one page then use my own template system to transfer the template to all the other pages. I only really use includes for like db settings, global config settings, functions etc. For html I leave that for the template system
    Can you elaborate on this a bit? How exactly are you implementing this (a very simple example is fine).

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Simplified version. That'd be one page, the template page is stored in another folder elsewhere and is put onto the page by the template function at the bottom once everything else has been completed
    PHP Code:
    <?php
    function sensitiveconfig(){
       
    db details here or any other details not to be seen
       grab file from below doc_root
    }

    function 
    globaluses(){
       
    checkfilterstoreclean variables used oftenvalidate form entriesurlsetc
    store navigation in an 
    array here
    }

    function 
    Template($url){
       
    open html fileread itgrab variables and output to page
    }
    ?>
    The HTML file will look just like a normal html file, apart from I'd have variables in it for php to replace with things from my functions.
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>{{title}}</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <link rel="stylesheet" type="text/css" href="style.css" />
        <!--[if lte IE 6]>
            <link href="ie.css" rel="stylesheet" type="text/css" />
        <![endif] -->
    </head>
    <body>
        <div class="banner">
            {{banner}}
        </div>
        <div class="nav">
            {{navigation}}
        </div>
        <div class="content">
            {{content}}
        </div>
    </body>
    </html>

  • #5
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    And so I gather that your Template() function is parsing the HTML file and replacing the variables inside by searching out "{{" and "}}", correct?

    If that's the case then no include() statements will be used whatsoever, no?

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    I use includes very little. It depends on how I've built the site really. Every site I do I do it differently as I'm trying to find the easiest and best way.

    If I have a big site, I store all the info and html that doesn't change on each page in a db, I'd then use includes on a page to get all the info out
    PHP Code:
    <?php
    include 'global.php';
    // db querys here
    // echo out headers
    html here for different layouts on the page
    // echo out content
    more html
    // echo out footer and closing html page
    ?>
    global.php will have everything stored, aswell as other include files in there to separate info so it's not a huge include file I'd be working on

  • #7
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    He just showed you a real generic breakdown. If you think about it, you will be using includes, require (w/e) in your php pages. Your php page will put your page together by including the appropriate template to parse...

  • #8
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I guess what's really throwing me for a loop is the fact that if you use an include it means you get all the included content "as it comes" right? I mean, in a standard site you would at least probably have static headers and footers, with changing content in the middle, right? Then that would by definition mean that you would need at least two includes, correct?

  • #9
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    no, in the include files, use functions, then call the functions in the php page where you want them displayed. No need for loads of include files scattered around a page. It'll be messy and hard to update and remember where bits of pages are when you come back to it in 6+ months time

  • #10
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    AH! Now it's starting to make more sense! I was (dimly) on the verge of arriving at this conclusion myself, but here's what tripped me up about that: Wouldn't that way of doing it involve a lot of bulky print() or echo statements? Isn't that...ahem..."dirty"?

  • #11
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Not at all, but before I starting using templates it was. I had html code mixed in with php code in my functions and I would to this and that and return html. I don't do that anymore with templates. I cheat and use smarty for my engine so my pages look like this:

    PHP Code:
    require_once('modules/'.$module.'/'.$module.'.php');
    require_once(
    'classes/navigation.php');

    $focus = new $module();

    $smarty->assign("fields"$focus->fields); 
    $smarty->assign("module_name" $focus->module_name);
    ... 
    other ones
    $smarty
    ->display('modules/'.$module.'/templates/view.tpl'); 
    Then my template looks like this:

    Code:
    <div class="page_header"><img src="images/{$module}.png" class="inlineimg" /> {$module_name}: Home</div>
    <div class="bar-blue">&nbsp;</div>
    <br />
    .....
    Then any functions I include I call in my php page and the only html that I return is when I build out my forms. Where the field is marked as text, select, radio... w/e. That way I can just assign fields in my class files with codes and then pull the correct output instead having to type out every damn form I use .

  • #12
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I see... I was considering using Smarty myself. Before I do, though, I'm interesting if anyone knows how much it affects the server load...? I'm sure it's using a lot of it's own functions in the background...

  • #13
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Smarty is, as template-systems go, fast. It doesn't do run-time pattern-matching (well, it can be set to do so for development purposes, but can be turned off for production), but instead 'compiles' the templates into PHP code.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #14
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yea if I had time I would develop my own but I just don't. With all open source projects it is very bulky and you will probably not use everything they offer. But it is a very good templating system IMO.

  • #15
    Regular Coder
    Join Date
    May 2006
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    With all open source projects it is very bulky and you will probably not use everything they offer.
    See, I'm not sure what you mean by this. If I'm not using everything they offer then how is it bulky? Or are you saying that it all has to be imported and it will be wasting resources it doesn't need?


  •  
    Page 1 of 5 123 ... LastLast

    Posting Permissions

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