Posts tagged PHP
Smarty Templates… Get That Shit Out of My House
Dec 15th
If the title wasn’t strong enough for you, allow me to rephrase. Anyone that thinks Smarty is a good idea to implement should be shot in the face (or at least immediately removed from all developer responsibilities) because I’m tired of it junking up the tubes…
First of all there is absolutely NO reason anyone should use Smarty. The “Why Use It” page at Smarty says, “One of Smartys primary design goals is to facilitate the separation of application code from presentation.” That’s complete bullshit… there is no separation of application code from presentation with Smarty. In fact, in every implementation I’ve seen there is more logic mixed in with the templates than any other method… Including one template in a loop inside of another template is complete fail right off the bat and that’s the entire premise of Smarty.
Let’s break it down by their bullet points:
Designers can’t break application code. They can mess with the templates all they want, but the code stays intact. The code will be tighter, more secure and easier to maintain.
Look I’ve been doing this for 10 fucking years and I’ve never worked with designers that even break up their own designs (I know they exist) let alone dick around in templates… LET ALONE WHO KNOW TEMPLATING ENGINE LIKE SMARTY!
Errors in the templates are confined to the Smartys error handling routines, making them as simple and intuitive as possible for the designer.
OMG really? I can has ANOTHER error system to feed me where the problems supposedly are? Except this time it’ll be less robust and more cryptic?! And what happens when a developer removes an “assign” from the logic end? They get to yell at the designer to fix it? Who knows how to fix it anyway? The developer that doesn’t have to touch the template or the designer that doesn’t have to touch the logic? Some mythical breed of designer who not only knows Smarty but also knows PHP?!
With presentation on its own layer, designers can modify or completely redesign it from scratch, all without intervention from the programmer.
Oh you mean like designers do with regular XHTML/CSS in every other system? You know… by separating out your controllers from your view using… OMG… PHP?!
Programmers aren’t messing with templates. They can go about maintaining the application code, changing the way content is acquired, making new business rules, etc. without disturbing the presentation layer.
See the part above where this is accomplished by… PHP!
Templates are a close representation of what the final output will be, which is an intuitive approach. Designers don’t care how the content got to the template. If you have extraneous data in the template such as an SQL statement, this opens the risk of breaking application code by accidental deletion or alteration by the designer.
Here we see why Smarty is so fail… They put SQL statements in their templates before they created Smarty?! Way to be MVC guys! Which, I’m guessing, is what they’re trying to be with the Smary system itself? So essentially we had some people that didn’t know the first thing about the MVC design pattern up and decide to design a templating engine for PHP that would be more MVC like… awesome.
You are not opening your server to the execution of arbitrary PHP code. Smarty has many security features built in so designers won’t breach security, whether intentional or accidental. They can only do what they are confined to in the templates.
LOL, what?! Your designers don’t have access to all the files in the dev environment? and you don’t version control your source? and all your designers know how to code as well?! Look if your company / team falls into the three categories above there is no way a templating engine for PHP is going to solve your problems… it’ll just complicate things.
What have you accomplished?
So essentially what you accomplished by using Smarty is to do something which you already could have done using only PHP and that is inherently done in good MVC code. Except this time you get to load an entire templating engine… and here’s my favorite part. You get to load every single template into memory and then parse through all of it to look for special tags! And then when you find the special tags you need to perform their actions whether it’s printing a value, running some pseudo-PHP code contained in the template, or my personal favorite… loading another fucking template!!!
The first time I see a conditional to determine what additional template should be used (if any, ugh) inside a Smarty template I want to smash the developer that wrote it IN THE FACE!
Summary
Here’s the short version… By using Smarty you incur massive amounts of overhead by having to read every template into memory and parse through them all looking for special tags. You also introduce an additional pseudo language requiring developers and designers to know this additional language while making your code base less readable by anyone other than who wrote it. You increase development time for all projects because you now have to use extra methods, variables, etc… and you are completely dependent on another system for your code base to work…
For all of these negative aspects what do you get?! A code base which is less scalable and less readable… congratulations, you built a better mousetrap.
