Slashdot Mirror


PHP Template Engines?

kubed asks: "I've recently learned how to use PHP template engines to separate business logic from presentation. Some argue that template engines make applications easier to maintain and make for cleaner code. Others argue that template engines introduce unnecessary overhead and require too much additional processing power. Do the readers of Slashdot think that it is important to use templates or are they just an extra unnecessary layer? There are dozens of PHP template engines to choose from including Smarty, phplib, and bTemplate. Which template engines do you have experience with and which ones have the best performance?"

5 of 90 comments (clear)

  1. Not terribly useful by Anonymous Coward · · Score: 1, Insightful

    I use XHTML 1.0 Strict and CSS extensively, so when it comes down to it, there is very little in the way of markup to abstract away, and a template engine merely gets in the way.

    If you are constantly tripping over nested tables and font elements, perhaps you are solving the wrong problem by trying to rationalise that markup instead of using HTML properly in the first place.

    1. Re:Not terribly useful by Smidge204 · · Score: 3, Insightful

      It depends on your application.

      For example, a template engine is used in phpBB (as with many message board systems). There's a lot of very dynamic and conditional content on message boards, and I honestly can't see how XHTML and CSS can effectively handle it.

      By abstracting the markup from the data, you can also simplify the markup. For example, if you are generating a table with an unknown number of rows, you can define a single row in your template and the engine will duplicate it as required. Same thing if you even have multiple tables of multiple rows.

      Personally, I think phpBB's engine is a little overboard, but it's like that to be more flexible than the boards I maintain require. For example, I don't see the need to a separate language file for anything I do (although I understand it's usefulness). Similarly, I don't really care for the style table it generates in code from the database (although this makes changing the styles easier via web interface). So to save overhead I remove these features and just put the data directly into the templates.

      What I'm really getting at is: Use the right tool for the job. PHP Template engines are a Good Thing(tm) but definately not the Only Thing(tm)
      =Smidge=

  2. Just a rule set? by eddy+the+lip · · Score: 3, Insightful

    Given the apparent popularity of templating systems for PHP I'm likely in a minority, but I really don't see the point. Languages like perl need some way of getting variables and at least some basic controls structures into the HTML so you don't have to resort to multiple print statements (god, the bad old days...). But PHP does this all on it's own already.

    Granted, this is often horribly, horribly abused with all kinds of spaghetti code strewn about the presentation layer, but this is the developer's fault, not that of the language. There's absolutely no reason you can't implement an MVC architecture (or just put your main code somewhere other than your presentation layer) without resorting to a templating engine.

    As far as I can tell, the only benefit of PHP templates is that it forces you to keep your code somewhere other than in the presentation. This is offset by generally having the ability to drop out into PHP anywhere you want to in the template anyway. In exchange, you add another layer of complexity to your application, increase execution time, are forced to learn a new syntax and are (frequently) shoehorned into the way the template engine thinks you should structure things.

    It's also often mentioned that it's easier on non-coders if you're handing the templates off to someone else for markup. But I really don't understand why (excuse the lack of indintenting - slashdot didn't like it)

    {if $user == "admin" }
    You can {$admin_option}
    {/if}
    is considered more intuitive than:
    <?php
    if ($user_type == "admin") :
    ?>

    You can <?= $admin_option =>

    <?php
    endif;
    ?>

    And if you're going to expose your HTML people to a tiny bit of code, it might as well be the actual language, which they may find useful someday. (Yeah, there's a couple more lines in the PHP example to suit my own formatting tastes).

    It seems to me that their only real purpose is to help enforce some kind of coding standards. I prefer to excercise a little discipline on my own. Nothing but variable expansion and control structures go into the presentation layer. The code that does the real work is elsewhere. If I'm overseeing others, I make sure they do the same. And god help them if they use a print statement for anything besides debugging.

    (Caching comes up as an advantage on occasion, but there are other options that don't involve marrying yourself to a template engine).

    I'll grant that I might be missing something obvious and wonderful. If I am, this is the place it'll be pointed out...

    --

    This is the voice of World Control. I bring you Peace.

  3. Re:I have mixed emotions by greenhide · · Score: 2, Insightful

    Granted, any language can be misused to create horrible code, but with rapid development with template languages, you have to be careful not to let the ease and simplicity of the language lull you into poor programming.

    In my experience, bad programming starts with the developer, not with the language. Languages that are more difficult to code in simply make the barrier to understanding a little higher, so they're *more likely* to be used by someone with good programming skills.

    Remember: The main goal of programming is to ensure that the end result solves the problem presented. Elegant code that doesn't result in what you want is probably worse than slightly ugly looking code that actually works.

    I've encountered plenty of programmers who are skilled at coding, but aren't good at paying attention to the needs of the project, so they spend time on the wrong elements, implement features that aren't needed, and omit features that are needed.

    The advantage of more flexible coding (which is equally possible with ColdFusion as it is with a more complex language) is that it is faster to make changes to the system. If everything was hardcoded or overly-coupled, then changes will be difficult. However, for well coded ColdFusion applications, changes are very, very easy to make -- much easier than a similar implementation in a "real" language.

    I've been coding in ColdFusion for a long time and I won't claim that I've never written bad code, but the language itself allows for applications that are very well written, robust, fast, and easy to update and change.

    Fusebox is the most popular programming methodology for ColdFusion, and they have versions of it for PHP as well now. I'm not sure how well it works with PHP, but I do know that it does a fantastic job with ColdFusion.

    --
    Karma: Chevy Kavalierma.
  4. Re:Template Engines are bad by Fletch · · Score: 3, Insightful
    Better for your designers learn css than make them deal with some half-assed half-HTML, half-PHP template.

    1) With PHP templates
    --
    * programming logic in php files
    * content in a database
    * structure/design in template

    2) With no templates but using css
    --
    * programming logic & document structure in PHP files
    * content in database
    * design in css

    Two is cleaner, no?

    No, not really. I mean, from which end? Your .php files sure aren't going to be cleaner.

    Frankly, I think you're missing the point. CSS and templating are not mutually exclusive. Just as CSS helps us separate style from content, so does templating help us remove content from application logic.

    3) With templates and CSS

    • application logic in .php
    • document structure in .tpl
    • style in .css

    I prefer #3.