“Paul M. Jones”:http://paul-m-jones.com/, author of “PHPSavant”:http://phpsavant.com/ announced version 2.3.1 on Saturday, along with a a few comments and criticisms regarding what some people would consider its only “competitor”, “Smarty”:http://smarty.php.net/.
Those that know me and my programming know that I have been a long time Smarty user. It’s mix of simplicity and flexibility has always excited me. As the feature list for Savant2 began to get longer and longer, I considered, on several ocassions, switching. Unfortunately, in many, cases, my applications involve template designers I cannot trust. I mean that in the sense the Paul explains:
bq. If the [template] author is an actual security risk; say, if your application allows its users to edit templates via a web form… then, and only then, is compiling or converting a limited markup to full PHP a reasonable option.
This reason alone is what kept me from ever considering PHPSavant. I didn’t want to get into a situation where I would use PHPSavant for those projects that didn’t have user editable templates, and use Smarty for those that did. In my opinion, I’m better off learning the ins and outs of one and using it everywhere it makes sense than I am to use different engines in different circumstances. That is, of course, unless the performance gains are really worth it.
I never actually benchmarked either application. But, a simple line of thought seemed to be enough to not require it. During runtime, PHPSavant executes the PHP based template code directly. Smarty does the same thing with one exception. On every template invocation, Smarty will stat a file. If that file is newer than the PHP based template code, a lengthy compilation process will occur. Of course, this only happens the first time a modified template is accessed. Additionally, in a production environment, if the performance is an issue, the additional stat call can be avoided resulting in the template being compiled only if it hasn’t been already. With this in mind, and not considering any differences in the speed of the template engine after the template has been complied, both engines will do basically the same thing in the same basic amount of time. With that in mind, it seemed silly to even consider learning and using PHPSavant, knowing that, in those cases where untrusted users would be allowed to edit templates directly, I would still have to use Smarty.
As of a few verisons ago, PHPSavant has a compiled templates option. In the event that your security needs would keep you from allowing template authors direct access to PHP, you can author a PHPSavant template compilation class that will, when activated, convert template markup of your own design into proper PHPSavant template code. With this feature, PHPSavant and Smarty move closer together in features, however, there are still two rather large, in some cases, issues that separate them.
Smarty has the advantage over PHPSavant because the template compiler and, therefore, the language of the template is already written. This means, first and foremost, that I don’t have to write a full-featured, flexible template compiler like I would with PHPSavant. This also means, though this is less important, that users of differnet Smarty based applications can join forces to teach one another and lead by example when it comes to what Smarty is capable of. With PHPSavant, those authoring templates in PHP would be able to help one another. But the users of applications that employed PHPSavant with a template compiler would find themselves in less company.
PHPSavant, however, also has a few distinct advantages over Smarty. First and foremost, PHP savvy users who are not security risks can use all of the features and tools built into PHP when authoring the logic portions of their template code. While Smarty does indeed provide a mechanism to allow the applicaiton programmer to give access to various PHP features to the Smarty template author, the PHPSavant template author already has every tool available to PHP at his disposal. Additionally, and this is much less of a concern as the needs rooted in template logic are fairly basic, if there is a feature, function, or logic construct that isn’t available in Smarty that is needed one must know the internal workings of Smarty, or wait on someone else to author this ability for them. Since the PHPSavant user is using pure PHP, as long as he knows how to program this functionality in PHP, he’ll be required to wait on no one.
In my experience with Smarty, I have *rarely* run across a construct that wasn’t available to me that I had a difficult time of finding a different way to do it. In the few cases where that did occur, I’ll admit that the Smarty code was less than ideal, and way less than pretty. But, it worked. Never have I had to alter the Smarty compiler to add a feature that wasn’t thought of before. Before Smarty had “foreach” functionality, I considered it, but, to my delight, “foreach” was added in the very next release.
So, to summarize, PHPSavant and Smarty, as this point, offer the same basic features and most of the same advanced features, with differing implementations. The biggest differences between them is that PHPSavant will require you to write your own template compiler if you need one, while Smarty will not let you write your templates in plain PHP. However, with the very small need that I’ve seen to use PHP functionality that wasn’t availble already in Smarty in some fashion, PHPSavant’s advantage isn’t THAT great. And since, in the end, a Smarty template gets compiled into PHP anyway, it’s only the first hit, and any differences in the speed of the two engines, the really make PHPSavant stand out.
On the flip side, if PHPSavant were to implement a template compiler that was as featureful as the Smarty template language and provide a means in which to request template compilation only if a template had been changed, Smarty would, unless there are any major speed differences, have no reason to exist whatsoever as PHPSavant would cover the needs of both the “pure PHP template junkies” and the “template tag sugar addicts”.
I’d love to hear your thoughts on this.
(And, as a side note, Paul also mentions that “caching seems more like an application-level task, not a template-system task” when talking about the “cache” of the compiled templates in Smarty. I couldn’t agree more with him. I’ve never used Smarty’s output caching features and find them to be in quite a silly location in the application flow. While the caching of a compiled version of a template is surely something that Smarty should handle, caching the final output of that page, and all the logic that goes with determining if a cache should be used or if a fresh page should be fried is something that should be left entirely up to the application.)
[...] PHPSavant vs. Smarty Something for me to read later (tags: php savant smarty) [...]