SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: Templates??

  1. #1
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Templates??

    I read an article/discussion about templates. Some said that templates are dead and some said no, they are good. What do you think? Should I use templates? If yes which template do you recommend (i.e. patTemplate, FastTemplate, Smarty, etc.)

  2. #2
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do whatever you like. Simple.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  3. #3
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anarchist!

  4. #4
    SitePoint Member
    Join Date
    Nov 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you don't have anything useful, please don't write. I am trying to learn PHP developing with templates.

    I know this post of your is counted for but this unethical.

  5. #5
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Seriously, lighten up. I doubt either of us were posting just to "raise our count".

    You want advice, I would suggest re-reading the other discussion about templates. This thread will likely only see the same debate relived, because the answer is that there is no one method that everyone agrees on.

  6. #6
    No. Phil.Roberts's Avatar
    Join Date
    May 2001
    Location
    Nottingham, UK
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I meant was:

    Do YOU want to use templates? You YOU need to use templates? Would you gain or lose anything by the use of templates? Would anyone need templating in order to use you code? Does that last question even apply?

    So, like I said, weigh up you needs, options and preferences, and then do what you want. At the end of the day which system you actually go with is irrelevent.
    THE INSTRUCTIONS BELOW ARE OLD AND MAY BE INACCURATE.
    THIS INSTALL METHOD IS NOT RECOMMENDED, IT MAY RUN
    OVER YOUR DOG. <-- MediaWiki installation guide

  7. #7
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't use templates like Smarty, fastTemplate etc. because I found it easier to just use PHP as the template engine

    Read the voostinds long (and famous) post in the first link and you will know why.

    If you want some arguments, read:
    http://www.sitepointforums.com/showt...threadid=67849
    http://www.sitepointforum.com/showth...threadid=69689

    or do a search

    -Helge

  8. #8
    Fully Qualified Fool :) luke-innovative's Avatar
    Join Date
    Jun 2002
    Location
    Kent, UK
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup, I agree, I think the best solution is just to write your own script - Template engines are pretty unneccessary
    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?"

  9. #9
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the template language is very extensive and complex, you have just replaced PHP with yet another template langauge. Template language should be easy with a few tokens and fast. In Eocene framework, I use a template but the template language consists of only five tokens to cover almost every thing, even very complex page layout



    [Advisor edit - Self promotion removed]

  10. #10
    SitePoint Zealot
    Join Date
    Dec 2001
    Location
    Ontario, Canada
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Personally, I really like the idea of domTemplate, which uses PHP's domxml module. Here's an example:
    http://www.sitepointforums.com/showt...096#post608096

    with this, there is no special syntax or tokens, its just plain XHTML.
    Web Hound
    $x='010000010110001101101001011001000101001001100101011010010110011101101110';
    for($i=0;$i<strlen($x);$i+=8)print(chr(bindec(substr($x,$i,8))));

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have happily been using template engines for a while now, no matter what rantings there are against them. I can only agree with:
    a) 'native' PHP code is faster
    b) template engines shouldn't be bloated mechanisms such as smarty (don't get me wrong, it's nice, but impractical for me)

    When I first started working for the company I'm currently working for, they did not use a template engine for their PHP things. That in itself was not a problem, except for this: they needed a developer every time the design changed a slight bit, simply because they didn't want to take the risk of breaking things (something that e.g. Dreamweaver tends to do quickly, and yes, they use Dreamweaver). If I let designers change things to the PHP code (even moving a piece of code), my experience is that 9 out of 10 times they go "oops, it doesn't work anymore". That is understandable, designers are not supposed to touch that code imho.
    Since I introduced templates here, things started to run a lot smoother, we can work in 2 ways:
    a) I create the system by the specs, make raw templates for it and pass them to the designer who does whatever he likes with it
    b) I get a complete design and include template code into the design without ending up with a dramatic piece of clutter
    As far as voostind's rants go: I don't want to break a certain religion/hero-worshipping here, I just find most of them irrelevant. My "will for arguing" is a bit rusty, I just want to use what's best for practical usage. So, to come back to the original question: if you work with 'pure' designers it might be a big improvement to use templates. Just do not use a template engine that takes a long time to learn, is hard to maintain and is a PITA to set up. Use an easy template engine you feel comfortable with and has a relatively good performance.

    Oh yeah, don't buy opinions like this easily. Think for yourself and what's most practical for you. A lot of opinions on such techniques sort of become religious laws made up by one or two persons, while they might not be familiar with your situation so you might just end up believing them and going a harder way because of it.
    Last edited by daholygoat; Dec 2, 2002 at 05:18.

  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use an easy template engine you feel comfortable with and has a relatively good performance.
    which one did you choose?

  13. #13
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use a modified version of FastTemplate nowadays, but there are plenty of template engines that behave similarly, for example:

    UltraTemplate
    TemplatePower
    patTemplate
    ecTemplate

  14. #14
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I let designers change things to the PHP code (even moving a piece of code), my experience is that 9 out of 10 times they go "oops, it doesn't work anymore". That is understandable, designers are not supposed to touch that code imho.
    Say you're using Smarty and you have a {foreach...} tag. Wouldn't the same thing happen if one of your designers placed the {foreach} tag at a wrong place in the template? The template wouldn't work either.

    I think there's a crucial thing regarding templates and PHP we need to keep in mind: using a PHP template does not mean spaghetti code with mixed HTML and PHP.

    You shouldn't mix PHP 'logic code' in your templates, for example you shouldn't make database connections in your template file. The only PHP code that should be in the template IMHO is code like simple if/else statements, foreach to loop through a set of values and functions like ucfirst(), number_format() to format the output. i.e. Presentation Logic.

    If, when your designers change a template, they mess up the whole script, you are not using PHP templates correctly.

  15. #15
    SitePoint Enthusiast
    Join Date
    Nov 2002
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Captain Proton
    You shouldn't mix PHP 'logic code' in your templates, for example you shouldn't make database connections in your template file. The only PHP code that should be in the template IMHO is code like simple if/else statements, foreach to loop through a set of values and functions like ucfirst(), number_format() to format the output. i.e. Presentation Logic.
    I agree.

    template engines force you to only use presentation logic in your templates but at the cost of added complexity and a speed decrease

  16. #16
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say you're using Smarty and you have a {foreach...} tag. Wouldn't the same thing happen if one of your designers placed the {foreach} tag at a wrong place in the template? The template wouldn't work either.
    Like I said, I don't use smarty. I don't want that kind of logic in templates.
    If, when your designers change a template, they mess up the whole script, you are not using PHP templates correctly.
    Thanks for the info, oh guru. Forgive me when I simply disregard that statement.

  17. #17
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you disregard a statement that basic, then you are disregarding the whole point of templating in the first place. Sheesh!

  18. #18
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you disregard a statement that basic, then you are disregarding the whole point of templating in the first place. Sheesh!
    Not planning to go into childish discussions here, but here
    If, when your designers change a template, they mess up the whole script, you are not using PHP templates correctly.
    you are basically concluding that it's my fault when a designer breaks a script? Interesting analysis. Anyhow, I've worked with both techniques, and I found my template approach the most convenient. Whether that's "not right" in your eyes or not.

  19. #19
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say you're using Smarty and you have a {foreach...} tag. Wouldn't the same thing happen if one of your designers placed the {foreach} tag at a wrong place in the template? The template wouldn't work either.
    Like I said, I don't use smarty. I don't want that kind of logic in templates.
    Alright, let me put it this way then.. In your template system, whatever you use, you will likely have some sort of 'block construct' to display one piece of HTML several times for a set of data. I have no idea what syntax you use, but it may be something like this:

    <!-- BEGIN BLOCK products -->
    Name:{name}, Price: {price}
    <!-- END BLOCK products -->

    If one of your designers were to accidentally move one of those BEGIN BLOCK tags to a wrong place, your template would not work properly anymore.

    Now picture yourself using PHP for templating, you'd have something like this:

    <?php foreach ($products as $product) { ?>
    Name: <?=$product->name?>, Price: <?=$product->price?>
    <?php } ?>

    I quote myself: If one of your designers were to accidentally move one of those <?php foreach(.. ?> tags to a wrong place, your template would not work properly anymore.

    See my point? Using a templating system does not prevent this kind of errors.

    you are basically concluding that it's my fault when a designer breaks a script? Interesting analysis. Anyhow, I've worked with both techniques, and I found my template approach the most convenient. Whether that's "not right" in your eyes or not.
    Sort of. Of course it is the designer's fault for having moved some template code to a wrong place, but as I pointed out above, that can happen with PHP code as well.

    If I let designers change things to the PHP code (even moving a piece of code), my experience is that 9 out of 10 times they go "oops, it doesn't work anymore". That is understandable, designers are not supposed to touch that code imho.
    In such a situation you are using PHP templates incorrectly: you have a type of code embedded in the template that should not be there, i.e. code that is not meant purely for presentation purposes but for logic.

    Thanks for the info, oh guru. Forgive me when I simply disregard that statement.
    I'm honored... Please, I am not trying to enforce some sort of pragmatic opinion upon you, I just went into discussion with you over an argument (for using a template system other than PHP itself) that was invalid IMHO.

    Not before long, I was using templates myself too, thinking they did the almighty "separation of presentation from logic" until I read some posts on this forum. I didn't just take over someone else's opinion, but I thought a lot about the arguments I read and I came to the objective conclusion that they were right.

    And I have to tell you, I am still using a template system on top of PHP, but for very other reasons, including that I want to prevent template designers from accessing my database for illegitimate reasons (the script is running on a server with multiple sites/webmasters).

  20. #20
    SitePoint Guru
    Join Date
    Nov 2001
    Location
    Fort Lauderdale
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Okay i'm totally lost

    I thought templates were just like site layouts that can be accomplished by making tables, and html.....

    So now there are launguages for making templates... or am i just really confused and tired??

    Where can i go to learn more, and can someone explain this to me alittle better as well as da advantages??

  21. #21
    SitePoint Guru
    Join Date
    Nov 2001
    Location
    Fort Lauderdale
    Posts
    814
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    anyone....

  22. #22
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought templates were just like site layouts that can be accomplished by making tables, and html.....

    So now there are launguages for making templates... or am i just really confused and tired??

    Where can i go to learn more, and can someone explain this to me alittle better as well as da advantages??
    You're right that templates are like site layouts. When you use templates in a program like Macromedia Dreamweaver, you can use them to maintain a consistent site layout.

    But templates are also a way to separate the HTML output of a script from the code that accesses data from a database and stuff. This can be done by putting the HTML in a separate PHP file, but in the past people have begun to use templating systems such as FastTemplate and Smarty because they think that if they use regular PHP templates, they are not "separating presentation from logic".

    That's what this discussion is about, whether to use a separate templating system on top of PHP or not.

  23. #23
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by 4SeeN
    Okay i'm totally lost

    I thought templates were just like site layouts that can be accomplished by making tables, and html.....

    So now there are launguages for making templates... or am i just really confused and tired??

    Where can i go to learn more, and can someone explain this to me alittle better as well as da advantages??
    I will try to explain the basics to you.
    A template file (no matter what templating system you use), in it's purest form would contain nothing else than the HTML code (that's why people are using the term "presentation layer). But of course with a site being dynamic etc.. this is not really possible. You somehow have to get the data into your template file. That's where templating system's come into play.

    A simple example using Smarty
    PHP Code:
    //This is your index.php file
    <?php
    $smarty 
    = new Smarty();

    //let's assume you have done a select query which returns an associative array (that would be $result)
    //the array looks like this
    // 'name' => '4Seen', 'age' => '23', 'location' => 'somewhere'

    foreach ($result as $key => $value)
    {
        
    $smarty->assign($key$value);
    }
    $smarty->display('index.tpl');

    // this would be your index.tpl file with all the HTML code
    <html>
    <
    head>
    <
    title>Hello World</title>
    </
    head>
    <
    body>
    // now you can access all your data from the database like this

    //put in your fancy html coding
    <p>{$nameis {$ageyears old and lives in {$location}.</p>
    </
    body>
    </
    html>
    And that's it. Of course you don't really need smarty to be able and achieve this, you can do the same with native php files.

    But that's something you have to decide for yourself

    I hope this helped somewhat.

  24. #24
    SitePoint Zealot marcoBR's Avatar
    Join Date
    Jun 2002
    Location
    Brazil
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  25. #25
    SitePoint Enthusiast Kymira's Avatar
    Join Date
    Sep 2002
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're forgetting some things. Smarty's reasoning for including logic in its template is one that makes sense.

    There are two type of logic that web apps depend on. The business logic (database connection, initializing sessions, data processesing, etc) and presentation logic (what color should this be? if the user is logged in, this is what should be displayed, if not logged on display this, etc).

    When I write an application, I don't care what the designer wants it to look like. BUT, the designer needs to have control over how it looks, and engines like Smarty give him/her that option.

    Plus, you're forgetting about the other benifts that Smarty provides, like page caching. You can do stuff like this using Smarty:

    (really rough code, little or no error checking, it's just to illustrate a point)
    PHP Code:
    $article_id = isset($_GET['article_id'] ? intval(trim($_GET['article_id'])) : '"":

    $cache_id = $article_id;

    if( !$smarty->is_cached('
    article_template.tpl', $cache_id) )
    {
        // psuedo code
        open_db_connection();
        run_article_query();
       $smarty->display('
    article_template', $cache_id);
    }
    else
    {
       $smarty->display('
    article_template', $cache_id);

    Smarty provides a way for me to figure out if a page is already cached. If it's already cached, all I do is display the cached version.

    I didn't even have to open a database connection because I knew which article I wanted to view ($article_id).

    The fact that I can save on database connections and queries makes Smarty, and other template engines like it, a wonderful and powerful resource.


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
  •