ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  The Dynamic Duo of PEAR::DB and Smarty
Subject:   Do not use smarty
Date:   2005-02-23 17:45:15
From:   mitchenall
Response to: Do not use smarty

Klaus,


It seems you have misunderstood the usefulness of template engines such as smarty. What smarty, and similar template engines, provide lets you decouple your core application logic from the presentation layer.


Making a weak statement like 'do not use any template system unless you have e.g. a big magazine which needs to change design very often.' is a ridiculous and absurd. How often do big magazines change their design? I think you'll find it's very little. In fact, if you're producing a site which changes design this often, you should be looking more at CSS, not just using template engines as this will give you more flexibility than a template engine will ever provide.


However, if your application logic is being re-used in a lot of applications, which is the case with a huge variety of websites and web applications, you can easily re-use your core code, just getting designers to change templates on a per-site basis, thus reducing costs associated with building new sites. Unless, of course, you like to rewrite all of your code for each new site you do and charge your clients accordingly. Maybe you want to keep yourself in a job by having to always write new PHP code mixed with HTML which nobody can maintain, rather than reusing previously written code and just getting the designers to work on the HTML side of things.


You comments about introducing an additional level of indirection seem also incorrect to me. Smarty decouples the presentation of data from the logic which outputs the data. This is surely a good thing. It provides a separate layer for designers and programmers to concentrate on the presentation side of website, without having to get too involved with the core logic which produced that data.


I know the arguments about template engines being just as complicated as the PHP code we'd otherwise use with HTML stuck in the middle of it, but as far as I'm concerned, template engines used correctly provide a useful additional layer which ultimately makes more code re-usable. Used incorrectly, they add a level of indirection which makes the overall application slower and harder to maintain.


One problem with this article is that the author has shown few of the benefits of using template engines such as Smarty, but in order to do so, he'd have had to write a much larger article.


Another one of your comments is also wrong as far as I'm concerned, i.e. 'The chain from the caller to the output becomes unnecessarily long.' This is utter nonsense. Used correctly, the chain between the caller and output is actually greatly shortened, and the amount of information passed from the caller script to the template is greatly lessened, especially if you make good use of custom plug-ins for outputting content which is of no concern to the calling script.