SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Evangelist John D's Avatar
    Join Date
    Jun 2003
    Location
    Derry, Ireland
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Whats the best way to do this?

    Hello,
    Im looking to add different section to my pages, ie.
    A left section with a mney, centre with the main text, eight with news/updates and a head and footer.
    At the minute im just including each HTML page through one PHP page,
    Im very new to PHP and this is the only wany I know.
    Are there any other ways of doing this?
    Thanks,
    John
    CanaryHotspot.com - Canary Island forum and information
    InternationalChatForum.com - International Travel Chat Forum and information

  2. #2
    SitePoint Addict Ghz's Avatar
    Join Date
    Jun 2002
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is what I usually do.
    I make the index HTML page with a big table in it. Then I devide that table into many sections as many as I want. In your case you will need to devide it into footer, header, body...etc. In each section you will need to put a unique word which will be replaced later with your HTML pages. That is to say, put the word {body} to replace it with the HTML output later, and {footer} to replace it with the footer later on and son on.
    I'de rather this way more than deviding my index page into frames which will take along time to be loaded.

    Regards,

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically then any kind of Template would do. I've posted script before on this, so have a look through the forum archives ?

  4. #4
    SitePoint Zealot Egghead's Avatar
    Join Date
    Feb 2002
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you have seen, Ghz is pointing you in the direction of a templating system, which has advantages and disadvantages. I once saw a pretty scathing attack on templating systems by one of the regular Advanced PHP forum guys. If I ever find it again I'll point you to it but basically, it seemed to suggest avoiding template systems and using a straight forward 'includes' method.

    Personally I don't quite know if I sit on the side of the fence which advocates or attacks template systems yet. Maybe I never will, but I am certain that some template systems can be quite complicated and my life is already complicated enough thank you. Like all things I think it is best to follow the K.I.S.S. theology.

  5. #5
    SitePoint Evangelist John D's Avatar
    Join Date
    Jun 2003
    Location
    Derry, Ireland
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I will just stay with the include for now then..
    Like i said before, im new to PHP so if the template system is too difficult then it would be better staying with the include.
    Thanks for all the help,
    John
    CanaryHotspot.com - Canary Island forum and information
    InternationalChatForum.com - International Travel Chat Forum and information

  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't think of any member from the advance forums who'd actually recommend using INCLUDEs over Templates; myself I'd recommend Templates every time. Depends on what uses you have for your website, for example, Smarty would proberly be overkill in your case ? Way too complex for your needs...

  7. #7
    SitePoint Addict Ghz's Avatar
    Join Date
    Jun 2002
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah me too, I would use the template system since it's the best way to me. I got used to use the template system so you can shoose yours depending on your need and how much php you know.


  8. #8
    SitePoint Zealot Egghead's Avatar
    Join Date
    Feb 2002
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dr L (I presume):
    I believe it was an article on The PHP Guy website which discussed the use of templating systems, (http://www.thephpguy.com/), which seems oddly to have disappeared into the ether. It promoted lots of thoughts and obviously stimulated the grey matter for some (See HarryF's site http://www.phppatterns.com/) - By the way, I'm not sure if Harry was the author. I'm too scared to ask because I'm a softy pants.

    I don't really remember much but reading through some of the posts on phppatterns.com I get the impression it was written quite some time ago and was more anti-Smarty than anti-templating but perhaps I'm wrong (again).

    I also get the impression that the case against templates was really because the "Designer" was once again getting control over the development process rather than sticking to drawing pictures and leaving the coding to "Developers". The arguement being one of seperating content from code. (I guess this is an important issue to some people too. Again, I could be wrong?)

    Stop press: I think this is the article (http://www.phppatterns.com/index.php...eprint/4/-1/1/) by HarryF. Apologies to Harry for dredging up old material and shameless plugging of website etc. Maybe some new thoughts on the subject could put the case for or against?

  9. #9
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For info, here is point and counter-point.

    There is a good discussion on the first article here.

    HTH

    Regarding templates, I fall firmly into the Smarty camp (so you can be aware of my bias )
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  10. #10
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That article of mine is a little provocative (these days I've mellowed a little). There's one more recent article of Zend: http://www.zend.com/zend/tut/tutorial-Darrell.php which makes the case (partly) against templates. The author needed to discuss output buffering as well IMO which is required if you're going to use some sort of framework (such as Jason does with struts ).

    I think there's still a ton of confusion about templates and many people (often wrongly) citing some notion of N-Tier and layering software.

    My main resistance to using any of the template systems currently available in PHP is that they waste my time with writing exhaustive scripts that bind perfectly good PHP varaibles and control structures to the template.

    Anyway avoiding another rant, I think the ideal model for templates (if you have to use them) has been defined.... by Microsoft with ASP.NET (shock horror!).

    If it's any proof that MS have done something very clever with the ASP.NET template syntax, bear in mind that Sun are now playng catch up with Java Server Faces.

    I'd seriously recommend anyone working on projects that deal with the presentation layer in PHP (e.g. Smarty nudge nudge Jason ) to research what MS has done. It's perfectly possible to do the same in PHP (as my humble prototype here hopefully demonstrates) and there's alot to be gained. Smarties approach to "compiling" a template into native PHP is the right way to go but we need to throw out obscure (and unnecessary) template syntax like;

    Code:
    {if foo=bar}
       Hello World!
    {endif}
    And start thinking;

    Code:
    <php:label id="myLabel" hidden="true"/>
    PHP Code:
    if ( $foo == $bar ) {
        
    $myLabel->add('Hello World');
        
    $myLabel->hidden false;

    Anyway, back to Johns original question at the start - have a look at output control functions and stick with the includes.

    Here's how you could go about solving your problem;

    PHP Code:
    <?php
    // Some query
    $result mysql_query($sql);

    include ( 
    'views/result_table.php');

    echo ( 
    $result_table);
    ?>
    Now that included "template";

    PHP Code:
    <?php
    // result_table.php

    // Start output buffering
    ob_start();

    // Build a table
    ?>
    <table>
    <?php
    while ( $row mysql_fetch_array($result) ) {
        echo ( 
    "<tr><td>$row['column_1']</td><td>$row['column_2']</td></tr>\n" );
    }
    ?>
    </table>
    <?php
    // Store the output in a variable
    $result_table ob_get_contents();

    // Stop buffering and clean out the buffer
    ob_end_clean();
    ?>
    By capturing the output in variables like this you have complete control over what gets displayed. Elements of your page which don't change much could even be cached. Check out this tutorial on PEAR::Cache_Lite

  11. #11
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @HarryF: Off the top of my head, I think one could do both by writing a template pre-filter in Smarty. The pre-filter could take the <php:...> syntax and convert it into the more obscure Smarty syntax, and then hand the template over to the Smarty compiler class for conversion into plain PHP.

    Other people are using pre-filters for manipulating the image path in the templates to work better in WYSIKOWYG editors.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  12. #12
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have to admiot I'm completley flumoxed by the whole template gloop (hmmm, flumoxed, gloop, is my vocabulary gettiung better, [img]images/smilies/redface.gif[/img] nahhh).

    So there is a perfectly good langauge, PHP, that has perfectly goods ways of grabbing files from else where include() require() blah blah.

    Why the need to use somthing like smarty? What does it actually give you? I really don't get it! All that extra parsing and essentially the rewritting of PHP, to then be translated into PHP, then into the output??? I'm quite baffled!

    ::whoosh - goes over head:::
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  13. #13
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mythix
    Why the need to use somthing like smarty? What does it actually give you? I really don't get it! All that extra parsing and essentially the rewritting of PHP, to then be translated into PHP, then into the output??? I'm quite baffled!
    I pesonally perfer it from the HTML separation aspect. When I am grabing data from a database, I want to be looking at PHP code. When I am tweaking a page to be XHTML compliant, I want to be looking at HTML.

    I wrote pages for years in ASP that mixed code with content, and I will never go back (willingly ) (does this look familar to anyone?
    response.write("<td>" & rs[0] & "</td><td>" & rs[1] & "</td>")
    ...)
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  14. #14
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    And back to the topic at hand (which is page layout, not templates), I would use <div> tags for each of your sections and control their position using CSS. Take a look at the book advertized at the top of the page for more info
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  15. #15
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote pages for years in ASP that mixed code with content, and I will never go back (willingly ) (does this look familar to anyone? response.write("" & rs[0] & "" & rs[1] & "" ) ...)
    But your still doing that in essence, just instead of inserting PHP your inserting the smarty template language.
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  16. #16
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I view Smarty's syntax as a "macro language" for HTML. When I am looking at a template, the Smarty syntax is minimally intrusive, and I can focus on just doing HTML. When I am lookin at a PHP script, I can focus on the guts of data manipulation for my system. I think probably use of templates eventually boils down to a personal preference.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  17. #17
    WebAmoeba mythix's Avatar
    Join Date
    Aug 2002
    Location
    here
    Posts
    578
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't think of any member from the advance forums who'd actually recommend using INCLUDEs over Templates
    Persoanl taste, i can accept that, that makes sense.

    So why, Dr Livingstone, do you believe that the advanced PHPer forum is full of template lovers? A coincidnece that they all like it, or someting else I've gone and missed, lol.
    Laws are like sausages. You have much more respect for them if you haven't actually seen how they're made.

    http://www.webamoeba.co.uk

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm.... I didn't say that the Advanced PHP forum was full of Template lovers Merely following on from what was said at the beginning of the thread, yes ? HarryF - I think I'll pass on MS and their ASP .NET okay ? I'll hang about waiting for Sun to catch up as they've done a not bad job so far with Java ? Proberly get one over MS as well in the bargain IMO

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I once saw a pretty scathing attack on templating systems by one of the regular Advanced PHP forum guys.
    Okay Mythix ?


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
  •