Feed Sign in with OpenID OpenID

Simon Willison’s Weblog

Smarty Links

I’m using Smarty in a project at Uni at the moment, mainly as a tool to allow team members to modify different parts of the codebase without stepping on each other’s toes. I’ve seen some good arguments against templating solutions such as Smarty, based around the point that PHP is already a templating engine so the extra layer of abstraction just isn’t necessary. Never-the-less, I’ve been finding that Smarty dramatically improves my productivity even when I’m working on my own. Here are the links I’ve provided for my team members to help get them up to scratch on how it can be used:

This is Smarty Links by Simon Willison, posted on 22nd April 2003.

View blog reactions

Next: Introspection

Previous: Credit where credit's due

16 comments

  1. I thought that Smarty (and templating engines in general) looked attractive until I tried using it. It took me at least twice as long to write a simple web app as it would have otherwise.

    Lately, I've been coding a lot of object-oriented PHP (I know, PHP isn't a great OO language) and using a templating engine really doesn't lend itself well to using a nice object model.

    For example, I like to use an object model consisting of collection classes and entity classes. An example would be a Courses collection class that consists of several Course objects. Your Courses class would have methods such as loadBySemester(), etc. Your Course class would have the specific course details such as title, teacher, time, location, etc. All of your database code is tucked away inside these objects and then using them on your page is as simple as saying

    Location: <&= mycourse->getLocation() &>

    When you use Smarty, you have to send all of your variables to the template so that they can be accessed and displayed. You can send them one at a time (easy for a designer to grab and use) or with an array (arguably complex for a designer, depending on experience). The problem that I ran into was that I couldn't pass my objects to the template, but this makes sense - using an object and its iterator and getter methods is too complex for most designers and you're really starting to blur the lines to the point where you have to wonder why you are bothering. At any rate, to send over the data from my objects, I had to fill in big, nasty arrays, therefore defeating the whole purpose of my clean object model.

    Anyway, it sounds like you've already studied up on the pros and cons. In my case, using this technology added a lot of overhead and frustration to my development. Hopefully you'll have better luck. And hey, if Yahoo is using it - it must be alright.

    Jason Long - 22nd April 2003 20:30 - #

  2. IRL I've always asked Simon what the point of templating systems were. I never understood his interest in them, and I'd been of the opinion that the need to use them was done away with by a) nice clean semantic mark up and b) a nice clean MVC separation.

    Well working on the same coursework project as Simon, but in a different team I've been workingon a JSP/Java/Struts solution. Now I totally understand why templating systems are useful. I have the same HTML code for some of my pages in seven or eight places. This could be reduced to one with more use of includes, bit I'm already using them gratutiously.

    If I had to develop in PHP I think I'd probably look into the Struts port to PHP. I hear it is in existance but I don't know the level of development. The point is it has nice tags such as <logic:iterate name="myCollectionClass" property="collectionClassAccessorName"> which allow a designer to iterate through either a Collection, Map or an array (I think that's right).

    I hate to admit it, but Simon has been right that Templating Systems do have a place, and I blame my lack of experience in developing web sites to my short sightedness

    Swannie - 22nd April 2003 22:14 - #

  3. To be honest Simon, you can avoid problems with your group stepping on each others toes in one simple step (no pun intended)... make sure that you get lumbered with doing all the software implementation yourself!

    Andrew - 22nd April 2003 22:50 - #

  4. Jason, I'm actually using a sort of hybrid approach at the moment. I have a full object oriented framework representing the application logic - for example, there's a Calendar class which defines methods like addEvent, getEventsForDay and so on. The 'get' methods return objects which can be passed in to Smarty by the (procedural) "interface" scripts. It's not pure OO but then neither is PHP, so it seems to work pretty well.

    Simon Willison - 22nd April 2003 22:58 - #

  5. I've been an advocate of using Smarty templates for quite a while now and I whole-heartedly agree with what Simon says. Using templates to abstract the content of a site from the code makes things so much easier to maintain, and it helps in development too.

    I'm working on a "super" b2 weblog, called b2++. A number of b2 users and developers have in the past wondered why I use templates but the advantages become clear as soon as other people start hacking your code.

    Thanks Simon for starting that wiki too, it's a great resource!

    Donncha - 23rd April 2003 09:57 - #

  6. In regards to Jason Longs comment above, you can assign (or register) your objects and use them in the templates to access your data as opposed to filling arrays. That will keep your clean object model, and may end up being more efficient since you don't assign unused data. {$foo} (normal assigned var) {myobj->get var="foo"} (registered object) {$myobj->get('foo')} (assigned object)

    Monte - 23rd April 2003 16:49 - #

  7. Quoting Swannie: The point is it has nice tags such as <logic:iterate name="myCollectionClass" property="collectionClassAccessorName"> which allow a designer to iterate through either a Collection, Map or an array (I think that's right). And do you really think that designers could manage that kind of thing? In my book that's simple programming. I'd have to set it up, they'd open the page in Dreamweaver and b****r the whole thing. That's the problem with TAL as well, which otherwise I do like the look of very much.

    Peter - 23rd April 2003 17:31 - #

  8. Well I use the term designer loosely :) By designer I meant whoever gets the job of writing or maintaining the views.

    Personally I find writing UI's of any kind a pain in the arse, whether they be in a windowing system or a templating system. The must be an easy way of specifying them, I just don't know what it is. (I'm thinking there must be something similar to the PythonCard for websites.)

    Swannie - 23rd April 2003 19:57 - #

  9. By your new definition of designer that would be me :-) The designer I work with doesn't get much further than Photoshop mockups, so I do all the HTML translation. Although he's done website himself recently using Dreamweaver... but my job's still safe (webstandards anyone?) ;-) And yes, I agree entirely with the comments about PythonCard, it's absolutely superb. Now I'm just trying to find how to minimise python apps (wxPython/PythonCard) into the system tray on Windows :-)

    Peter - 24th April 2003 09:08 - #

  10. Oh, and Simon: could cite be styled please? I used it in my first reply to Swannie instead of blockquote as I felt it to be more suited, but it doesn't show up as any different.

    Peter - 24th April 2003 09:09 - #

  11. Regarding Jason's comment: - You have a clean object model - You pass the "big nasty data" to template - Template gets this data in a "simple to display format" and displays it who cares if the data is big and nasty? Your coding is still done in a nice OO and template is done in a simple way. What's the problem? Speed? Elegance? Memory Usage? How big is your data?

    Jim - 27th April 2003 17:41 - #

  12. Simon, what do you think of ezSQL? Would you use it for "normal" projects instead of PEAR::(M)DB or Adodb?

    Peter - 15th July 2003 16:32 - #

  13. Peter: It depends on the project. ezSQL is great for small applications that aren't going to be doing heavy database work, but falls down in that it loads ALL of the results from a query in to memory at once in a big array. For big result sets that could end up being a problem.

    Simon Willison - 15th July 2003 17:26 - #

  14. Hello.

    Just to say I use a different php template engine, Templeet.
    It's very easy to use; for example, I don't know about php coding, but I created my own site in templeet. It include caching, some core functions to simplify all the ennoying coding part of php (like db access), and support all the php functions. take a look at http://www.templeet.org

    Baptiste Mille-Mathias - 27th August 2003 23:25 - #

  15. The point of templating is often lost on programmers that do their own design (read: HTML). But in many large orgs and with back-end logic that may be applied to many different client's projects, it works very well. Think content management for large intranets or on-line newspapers.

    jbottero - 10th November 2003 23:39 - #

  16. Dave - 22nd September 2006 11:55 - #

Comments are closed.

Previously hosted at http://simon.incutio.com/archive/2003/04/22/smartyLinks

A django site