Smarty Templates Presentation & Recap

Last week, I gave a presentation to the DC PHP Developers' Group on Smarty Templates. The slides are now available in addition to some of the example code* used during the presentation. To get this code running on your system, go download Smarty Templates yourself and drop the classes into the lib directory.

* Sorry, not all the code is for public consumption and therefore has not been included here.

One of our local PHP'ers – Douglas Clifton – raised a number of valid points both before and since the presentation. I addressed a number of them during the presentation, but I'll share my thoughts here also. In no particular order and with a bit of artist license, here they are:

  • PHP is a templating language, why are you adding another?
  • My token replacement/concatenating string templating is just fine, why should I use another?
  • Oh great, yet another syntax, why are you making my life more difficult?
  • I've seen the templates in {a project} and they're riddled with display logic more complex than anything I'd like to consider.

Good points… all of them, so let's look at some of the underlying reasoning.

Points 1 & 2: Right off the bat, the first two questions are variations of the same, so let's consider them together. Yes, PHP is a great templating tool and I have and in small projects or on projects with long development cycles, this is likely to work indefinitely. I know because I've rolled my own in the past. It was a relatively simple exercise to segment the templates clearly, drop tokens in the correct places, and generate new layouts with minimal effort. I considered it a success.

As the requirements and complexity of the projects continued to grow, I found that I was falling behind. There were requirements and aspects of the system that my “Dummy Templates” system couldn't support. It had no concept of caching (styles or data), did not have a way to add “plugin” or functions cleanly, and rarely failed gracefully. Once I reached this point, I realized that Smarty fit the bill quite well, offered everything I was looking for, and quite a bit more. Luckily, when I created my little engine, I was heavily influenced by the JSP Tag Library notation and had to make few tweaks to make the conversion.

Also hidden in these first two questions is a completely valid criticism. If you roll your own system, it does exactly what you tell it to.Whenever you include third-party libraries, there is a certain amount of the functionality which you are unlikely to want or need at the beginning. That may change or it may not, that's something you need to evaluate yourself.

Point 3: Yes, this is another syntax. If you're already a PHP'er, you may not be familiar with the {$value} notation, but I suggest you make a point of it. Here are two code samples:

$value = "Hello $username, how are you today?";


$value = "Hello {$username}, how are you today?";

These are identical in functionality, in terms of parsing, in terms of just about everything I can think of. But I would consider one significantly more readable and recognizable at a glance. Conveniently enough, Smarty uses the {} notation and replaces all of your “echo” statements throughout your template. Combing that with a few simple constructs like foreach and if/else and you suddenly have a huge amount of power at your fingertips.

The flip side of this is that since the developer creates these items and passes them to Smarty, the designer can cause very limited damage. In an application such as dotProject, you have a very complex permissions system defining who can see which projects. If the designer can only use the project list the developer gives them, they can effectively forget that the permissions even exist. They only get “approved” data they're given, regardless of what they do. Think of the security implications of that one.

Point 4: Yes, templating systems can be abused or used badly. The next thing you'll tell me is that someone might try to use a spreadsheet as a database, a cell phone as an mp3 player, or even a car as a status symbol. Unfortunately, there's nothing I can do for you there except to encourage you to rigorously review your code reguarly. If you see complexity somewhere that doesn't make sense, check it out and see if it can be simplified.

And the point is…. ?

A templating system – Smarty or not – might make your life easier. It might fit your requirements and fuctionality without introducing unnecessary cruft and complexity… or it might not. Luckily for me, that's your decision.