PHP and Templating Languages
Recently I’ve been spending a little more time in PHP land and coming across some various tid bits - I freely admit that I’m brand new to PHP and not steeped in it’s lore, but something that always confounded me was the purpose of SMARTY. Turns out SMARTY is a templating language that has garnered much popularity in the PHP world… this confuses my pea sized brain. PHP is already a templating language, why did they need another? I was gratified to read that there’s starting to be some talk about this very topic.
When I first came across SMARTY I thought that perhaps it was super deadly simple so that anyone could use it without any fancy developer knowledge. Of course, then, the templating language needs to trade flexibility for simplicity - as soon as you start putting in looping and conditionals you have a whole new programming language. This is the problem almost every templating language has - why do they need to exist? They aren’t simpler than other languages, they are simply less powerful and worse.
In SMARTY’s case, for example, sure this:
Name: {$name}
Address: {$address}
Is marginally better (and by better, I mean less typing) than what it would be in PHP:
Name:
Address:
But, uh… what the hell does this mean?
<table>
{section name=mysec loop=$users}
{strip}
<tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
<td>{$users[mysec].name}</td>
<td>{$users[mysec].phone}</td>
</tr>
{/strip}
{/section}
</table>
That sucks and no one who wouldn’t be able to do the same thing in straight PHP could do that on their own in SMARTY language. And don’t kid yourself that this isn’t a programming language, it has functions, operators, looping and all sorts of stuff. All sorts of stuff that is just worse than actually using PHP - because now you not only have to master one language you have to master two and your codebase is now in two languages.
This doesn’t do anything more to separate presentation logic from business logic, this doesn’t make it easier for your non-programmer designers to edit these pages. Just because you call it a templating language and change the token that divides the programming from the html doesn’t make it any less a programming language, just a worse one. It makes things more complicated. If you were really adamant about separating all this stuff into different files you could do the same thing by having one “programmer” file gather all the variables and what not and then simply include another PHP based “designer” file. There, now you have a full featured templating language that everyone knows.
I don’t know, maybe there’s something I’m missing. Maybe it’s just residual from a long line of not understanding all these weird templating languages, but just like Josh said… I don’t get it.







