Why PHP as a templating language sucks
I frequently read articles about why any template system made with PHP is per definition redundant because PHP is itself such a good templating engine. This is my take on this whole rant-genre.
One of the best posts on why PHP is the templating engine basically misses the heart of the issue, like all other pro PHP as a templating language posts/rants:
I’m basically advocating a “template engine” that uses PHP code as it’s native scripting language. I know, this has been done before. When I read about it, I thought simply, “what’s the point?” After examining my co-worker’s argument and implementing a template system that uses straight PHP code, but still achieves the ultimate goal of separation of business logic from presentation logic (and in 40 lines of code!), I have realized the advantages and honestly, can probably never go back.
True, this all works if you work in a company where you have good programmers who are all aware of the virtues of code and layout separation. This is not always the case and that is the problem with this line of reasoning. That is why I would like to state the opposite: PHP as a templating engine can suck big time!
It’s not a coincidence that most programmers I’ve met who can’t understand the point of Smarty are either very good or very bad. Given to their own devices using only PHP, the bad ones will quickly start to produce horrible level 1 code even when they were told to keep the logic separated from the layout. They simply don’t get it which is why they also have to be told that they can’t use the {php}{/php} Smarty tag.
Fair enough, the Smarty “language” is not really that much easier than PHP and our designers haven’t really learnt it to an extent that they can actually work with it autonomously. So in that respect it is a failure. However, as long as it can prevent spaghetti code it’s worth using.
Also from the above post:
…you get all the power of PHP without all the overhead of parsing a template file.
I’m not really bothered about the extra overhead but all that power can be a real headache!
Related Posts
Tags: php templating smarty