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.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Mar 2004
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Template Problem - plz help

    hi everyone,

    plz help me,
    I am not able to figure out that that how can i use templates with php.
    I dont want to use smarty templating system,

    Is there any other way. I am not able to find tutorial on the net.

    Plz help ASAP

    thanx
    gaurav Behl

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Why don't you want to use smarty? There's fasttemplate you could use, otherwise you're gonna have to script one yaself

  • #3
    New Coder
    Join Date
    Apr 2004
    Location
    NC
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    www.phpfreaks.com has some good tutorials on classes.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    What have classes got to do with template parsing?

  • #5
    New Coder
    Join Date
    Apr 2004
    Location
    NC
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I thought by templates he meant classes

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    simplest template setup ....

    template.tpl
    Code:
    <html>
      <head>
        <title>{title}</title>
      </head>
      <body>
        <div class="paragraph">
        <div class="title">
          {para1_title}
        </div>
          {para_1}
        </div>
      </body>
    </html>

    PHP Code:
    <?
    $tpl 
    implode'' file'template.tpl' ) ) ;
    $search=array( '{title}' '{para1_title}' '{para_1}' ) ;
    /*replace could come from your database/wherever*/
    $replace=array( 
      
    'welcome to the template' 
      
    'All your templates' 
      
    'belong to PHP blah blah' 
    ) ;
    $tpl str_replace$search $replace $tpl ) ;
    echo 
    $tpl ;
    ?>
    there are a thousand ways & variations , but simplistically you take a template and within that template put placeholders (above I use {placeholder}) , then you parse the template replacing the placeholders with your content.

    Now templates often require other forms of logic such as looping , e.g. you may have a variable number of paragraphs above and do not want to have to put all those placeholders in the template , rather loop through them.

    There are several approaches to this from putting code/pseudo-code in your template (which I personally do not like (e.g. defeats the object)) or make your template parser smarter , or , I often have several templates for a single page anything that requires a loop or other logic is parsed seperately and then I stick all the bits together, this can be useful for standardising content over several pages and not having to have several templates that have large sections that essentially do the same thing .

    so have a play with the above type of methodology for now as further expansion on templating methods is very subjective and the cause of many a fight
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What about using ob_start() and ob_end_flush()?? I have my template, and then only in a special part of my template I wana have all my pages to appear. So, I print a PHP variable in that part of the template, and then in all my pages I use ob_start(callback) & ob_end_flush() to get all the output of my page and then I put it in that variable. After that I include that template, because the template outputs the variable, then everything will be printed in its place. How's this approach?

  • #8
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    Its fine , I played with that once after seeing a post in the manual notes , but I got bogged down when I had to iterate over some sections , so I gave up and went back with what I knew , I am sure that there are ways around that since PHP lets you work with several buffers at once .. me just too lazy

    I also tried eval() for templates as well (I think thats how VB works ?) but with similar issues and nested eval()'s scare me~
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #9
    Regular Coder
    Join Date
    Jul 2002
    Location
    Iran
    Posts
    695
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, eval is very scary and I don't think you need to put all your php commands inside a variable too.

    About that ob_start() thing, one of my friends was telling me I shouldn't use it because it opens lots of streams and samples of my program and makes the server busy, but I couldn't see any down side, coz my script was running fairly fast. The only problem I have is that sometimes (just sometimes!) when I have some fatal errors in my page it can't show it. I mean I receive a blank page, it's because the ob_end_flush() doesn't get excecuted and it doesn't call my callback, only in that time I have to remove ob_start() and see my naked page (wihtout template!) and find out the error. Of course it doesn't happen that much. But it's really a great thing having templates! How easy the work becomes and how many copy & pasting you can save. I don't think I can ever code without templates!


  •  

    Posting Permissions

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