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:
- The Smarty Website
- The Smarty Manual
- The Smarty FAQ
- The Smarty Wiki (which I founded last year)
- The new Smarty Forums
- Zend’s first Smarty tutorial
- Zend’s Advanced Smarty tutorial
- The Dynamic Duo of PEAR::DB and Smarty (although we’re using ezSQL)
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 - #
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 - #
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 - #
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 - #
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 - #
Monte - 23rd April 2003 16:49 - #
Peter - 23rd April 2003 17:31 - #
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 - #
Peter - 24th April 2003 09:08 - #
Peter - 24th April 2003 09:09 - #
Jim - 27th April 2003 17:41 - #
Peter - 15th July 2003 16:32 - #
Simon Willison - 15th July 2003 17:26 - #
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 - #
jbottero - 10th November 2003 23:39 - #
Dave - 22nd September 2006 11:55 - #