<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>No Smarty</title>
    <link>http://www.nosmarty.net/rss/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>The main blog feed for my Web site.</description>
    
    
        <item>
          <title>Is Smarty right for me?</title>
          <description>&lt;p&gt;&lt;span class=&quot;date&quot;&gt;[08-Aug-2008]&lt;/span&gt; No.  Despite its name, Smarty is not the most intelligent solution available to you.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s true that Smarty has received &lt;a href=&quot;http://www.phpinsider.com/smarty-forum/viewforum.php?f=12&quot;&gt;numerous testimonials&lt;/a&gt; over the years.  However, it&amp;#8217;s clear that most of these testimonials are from developers who are unfamiliar with &lt;a href=&quot;http://en.wikipedia.org/wiki/Multitier_architecture&quot;&gt;multitier architectures&lt;/a&gt;.  For these users, the concept of separating business logic and presentation is (understandably) an innovative concept.  But Smarty is only one such way of achieving this separation, and no longer the best alternative available (if it ever was).&lt;/p&gt;

&lt;p&gt;Smarty has &lt;a href=&quot;http://www.smarty.net/rightforme.php&quot;&gt;a list of benefits&lt;/a&gt; on &lt;a href=&quot;http://www.smarty.net/&quot;&gt;its website&lt;/a&gt;.  Let&amp;#8217;s go through some of them and shoot them down.&lt;/p&gt;

&lt;h3&gt;Caching&lt;/h3&gt;

&lt;p&gt;Here&amp;#8217;s what Smarty says:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Smarty provides fine-grained caching features for caching all or parts
    of a rendered web page, or leaving parts uncached. Programmers can
    register template functions as cacheable or non-cachable, group cached
    pages into logical units for easier management, etc.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;OK, so let&amp;#8217;s take a look at Smarty&amp;#8217;s caching system.  To enable caching, you would write&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;$smarty = new Smarty();
$smarty-&gt;caching = 1;
&lt;/div&gt;

&lt;p&gt;This sets up caching with the default lifetime of 3600 seconds (that&amp;#8217;s one hour).  What if you want to use an alternate &lt;a href=&quot;http://en.wikipedia.org/wiki/Time_to_live&quot;&gt;TTL&lt;/a&gt;&amp;#8211;say, 30 minutes?  Then you would write&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;$smarty = new Smarty();
$smarty-&gt;caching = 2;
$smarty-&gt;cache_lifetime = 1800;
&lt;/div&gt;

&lt;p&gt;Oh, of course, &lt;em&gt;&lt;code&gt;$smary-&amp;gt;caching = 2;&lt;/code&gt;&lt;/em&gt;.  What else would it be?  Well, a constant instead of a &lt;a href=&quot;http://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constant&quot;&gt;magic number&lt;/a&gt; would be nice.  But wait, why does the TTL even depend on a magic number being set for &lt;code&gt;caching&lt;/code&gt;?  If &lt;code&gt;caching&lt;/code&gt; is going to be accessed directly through a public property of &lt;code&gt;Smarty&lt;/code&gt;, shouldn&amp;#8217;t it at least be a simple Boolean, either true or false?&lt;/p&gt;

&lt;p&gt;There are a number of headaches like these baked into Smarty.  Far from mere aesthetic concerns, this unintuitive design has a real effect on day-to-day usage when you are forced to refer to the documentation again and again for even the most common uses.&lt;/p&gt;

&lt;p&gt;Syntax isn&amp;#8217;t the only issue.  Caching with Smarty can be a challenge when serving a load-balanced Smarty-based application from multiple servers.  The cache file generated by one server cannot be reused by another unless a &lt;a href=&quot;http://en.wikipedia.org/wiki/Clustered_file_system&quot;&gt;clustered file system&lt;/a&gt; like GFS or OCFS is used.&lt;/p&gt;

&lt;p&gt;Let&amp;#8217;s take a look at configuration.&lt;/p&gt;

&lt;h3&gt;Configuration&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Smarty can assign variables pulled from configuration files. Template
    designers can maintain values common to several templates in one
    location without intervention from the programmer, and config
    variables can easily be shared between the programming and
    presentation portions of the application.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Smarty allows you to store files in &lt;a href=&quot;http://en.wikipedia.org/wiki/INI_file&quot;&gt;INI format&lt;/a&gt; and then load them.  There&amp;#8217;s a PHP function for this already.  It&amp;#8217;s called &lt;a href=&quot;http://us.php.net/manual/en/function.parse-ini-file.php&quot;&gt;&lt;code&gt;parse_ini_file()&lt;/code&gt;&lt;/a&gt;.  Even if you&amp;#8217;re resistant to using PHP in view templates, configuration loading should happen in the business logic, anyway.  (Incidentally, Smarty doesn&amp;#8217;t use this function, instead opting for its own parsing mechanism for no discernible reason.)&lt;/p&gt;

&lt;h3&gt;Security&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Templates do not contain PHP code. Therefore, a template designer
    is not unleashed with the full power of PHP, but only the subset of
    functionality made available to them from the programmer (application
    code.)&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Never think that an application is more secure simply because the template engine claims that it makes it more secure.  Any security gain you might see from turning off the &lt;a href=&quot;http://www.smarty.net/manual/en/language.function.php.php&quot;&gt;&lt;code&gt;{php}&lt;/code&gt;&lt;/a&gt; tag in Smarty is offset by the dramatically increased inflexibility for the template developers.  &lt;del&gt;Believe it or not, out of the box Smarty does not even allow a template developer to format a number.&lt;/del&gt;  &lt;em&gt;(Edit: Kaloyan K. Tsvetkov corrected us by pointing out that the &lt;a href=&quot;http://smarty.net/manual/en/language.modifier.string.format.php&quot;&gt;&lt;code&gt;string_format&lt;/code&gt;&lt;/a&gt; modifier does in fact apply to numbers.  May we suggest a &lt;code&gt;number_format&lt;/code&gt; alias, at least?)&lt;/em&gt;  The end result is that the back-end developers end up taking on a lot of the responsibility of the front-end developers, usually by creating plugins for such inane tasks as converting to an integer.  While there&amp;#8217;s no data on this, it&amp;#8217;s likely that most websites using Smarty have the &lt;code&gt;{php}&lt;/code&gt; tag enabled&amp;#8211;in which case, what&amp;#8217;s the point of Smarty in the first place?&lt;/p&gt;

&lt;p&gt;In any event, the most common security hole for websites today is not a result of front-end developers at all, but lazy developers who let unfiltered input modify data directly&amp;#8211;something that no template engine can possibly defend against. For real security, hire security-minded developers instead.&lt;/p&gt;

&lt;h3&gt;Easy to Use and Maintain&lt;/h3&gt;

&lt;p&gt;Smarty has this to say about its ease of use:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Web page designers are not dealing with PHP code syntax, but instead
    an easy-to-use templating syntax not much different than plain
    HTML. The templates are a very close representation of the final
    output, dramatically shortening the design cycle.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In our experience, Smarty dramatically &lt;em&gt;increases&lt;/em&gt; front-end development time&amp;#8211;even with the &lt;code&gt;{php}&lt;/code&gt; tag enabled&amp;#8211;because of its numerous unintuitive behaviors!&lt;/p&gt;

&lt;p&gt;But let&amp;#8217;s look at some examples of Smarty&amp;#8217;s syntax.  In the end, &lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;{$foo}&lt;/div&gt;

&lt;p&gt;is not significantly simpler to use or maintain than&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&amp;lt;?= $foo ?&amp;gt;&lt;/div&gt;

&lt;p&gt;And Smarty is demonstrably &lt;em&gt;not&lt;/em&gt; easy to maintain when&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&amp;lt;?= foo ?&amp;gt;&lt;/div&gt;

&lt;p&gt;prints &amp;#8220;foo&amp;#8221;, tipping you off to the fact that you meant to print a variable but forgot the dollar sign, and&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;{foo}&lt;/div&gt;

&lt;p&gt;prints nothing.&lt;/p&gt;

&lt;p&gt;When assigning variables, this&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;{assign var=&amp;#8221;foo&amp;#8221; value=&amp;#8221;bar&amp;#8221;}&lt;/div&gt;

&lt;p&gt;is certainly less readable than this&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&amp;lt;?php $foo = &amp;#8216;bar&amp;#8217;; ?&amp;gt;&lt;/div&gt;

&lt;p&gt;Of course, these are basic examples.  The real test of a syntax, be it Smarty or PHP, is how it handles more complex tasks.  In that light, this:&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;{capture assign=&amp;#8221;foo&amp;#8221;}{my_helper var1=&amp;#8221;bar&amp;#8221; var2=&amp;#8221;qux&amp;#8221;}{/capture}&lt;/div&gt;

&lt;p&gt;is initially impenetrable to new users, compared to something like this, which you might see in an &lt;a href=&quot;http://en.wikipedia.org/wiki/Model-view-controller&quot;&gt;MVC&lt;/a&gt; application:&lt;/p&gt;

&lt;div class=&quot;code&quot;&gt;&amp;lt;?php $foo = $this-&gt;myHelper(&amp;#8216;bar&amp;#8217;, &amp;#8216;qux&amp;#8217;); ?&amp;gt;&lt;/div&gt;

&lt;p&gt;(The most obvious solution, &lt;code&gt;{assign var=&quot;foo&quot; value={my_helper var1=&quot;bar&quot; var2=&quot;qux&quot;}}&lt;/code&gt;, is invalid, of course.)&lt;/p&gt;

&lt;h3&gt;Variable Modifiers&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;The content of assigned variables can easily be adjusted at
    display-time with modifiers, such as displaying in all upper-case,
    html-escaped, formatting dates, truncating text blocks, adding
    spaces between characters, etc. Again, this is accomplished with no
    intervention from the programmer.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As mentioned above, the idea that this is accomplished with &amp;#8220;no intervention from the programmer&amp;#8221; is optimistic at best.  Smarty comes with 22 of these modifiers, but &amp;#8220;the programmer&amp;#8221; will have to add a number of new ones in order for the front-end developer to do his job.  New requirements will continue to come up, again and again, and eventually the benefit of leaving the &lt;code&gt;{php}&lt;/code&gt; tag disabled will be called into question, resulting in it being enabled.&lt;/p&gt;

&lt;h3&gt;Template Functions, Filters, Plugins, and Add-ons&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Many functions are available to the template designer to handle tasks
    such as generating HTML code segments (dropdowns, tables, pop-ups,
    etc.), displaying content from other templates in-line, looping over
    arrays of content, formatting text for e-mail output, cycling though
    colors, etc.&lt;/em&gt;&lt;/p&gt;
    
    &lt;p&gt;&lt;em&gt;The programmer has complete control of template output and compiled
    template content with pre-filters, post-filters and output-filters.&lt;/em&gt;&lt;/p&gt;
    
    &lt;p&gt;&lt;em&gt;Almost every aspect of Smarty is controlled through the use of
    plugins. They are generally as easy as dropping them into the plugin
    directory and then mentioning them in the template or using them in
    the application code. Many user-community contributions are also
    available. (See the plugins section of the forum and wiki.)&lt;/em&gt;&lt;/p&gt;
    
    &lt;p&gt;&lt;em&gt;Many user-community contributed Add-ons are available such as
    Pagination, Form Validation, Drop Down Menus, Calander Date Pickers,
    etc. These tools help speed up the development cycle, there is no need
    to re-invent the wheel or debug code that is already stable and ready
    for deployment. (see the Add-ons section of the forum and wiki.)&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Smarty has many names for view helpers.  View helpers are useful, but there are better templating solutions available that support view helpers and have a wider range of features.  The truth is Smarty&amp;#8217;s default list of variable modifiers do little to speed front-end development, and many add-ons contain code that has no place in the template engine.  This is the result of improper separation of concerns.&lt;/p&gt;

&lt;h3&gt;Resources&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Templates can be pulled from any number of sources by creating new
    resource handlers, then using them in the templates.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&amp;#8220;Resources&amp;#8221; (really, storage adapters) allow you to serve templates from sources other than a file system.  Smarty cites examples including databases, socket connections, shared memory (e.g., &lt;a href=&quot;http://www.danga.com/memcached/&quot;&gt;memcache&lt;/a&gt;), and even LDAP.  LDAP?&lt;/p&gt;

&lt;p&gt;Caching your templates in memory is smart, but templates should always be kept on a local file system if at all possible.  Load them from the file system, &lt;em&gt;then&lt;/em&gt; cache to memory.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s commendable that Monte Ohrt, the developer of the feature, tried to generalize what was clearly a request from the community for the ability to serve templates from a database.  But PHP has supported &lt;a href=&quot;http://us2.php.net/manual/en/intro.stream.php&quot;&gt;streams&lt;/a&gt; since 2002 with the release of PHP 4.3.  Even though rewriting this feature to use streams would allow it to work seamlessly with existing stream code users may have written for this purpose, not to mention giving every Smarty-based application using resources a large performance boost, there has been no movement on this for six years.&lt;/p&gt;

&lt;h3&gt;Debugging&lt;/h3&gt;

&lt;p&gt;Smarty says this about its debugging capabilities:&lt;/p&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Smarty comes with a built-in debugging console so the template
    designer can see all of the assigned variables and the programmer can
    investigate template rendering speeds.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Far from a nice interface &lt;em&gt;a la&lt;/em&gt; Firebug, this is an unreadable 200-line data dump.&lt;/p&gt;

&lt;h3&gt;Compiling&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Smarty compiles templates into PHP code behind the scenes, eliminating
    run-time parsing of templates.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Smarty compiles down to PHP.  This should give you a hint.  Really.&lt;/p&gt;

&lt;h3&gt;Performance&lt;/h3&gt;

&lt;blockquote&gt;
    &lt;p&gt;&lt;em&gt;Smarty performs extremely well, despite its vast feature set. Most of
    Smarty&amp;#8217;s capabilities lie in plugins that are loaded on-demand. Smarty
    comes with numerous presentation tools, minimizing your application
    code and resulting in quicker, less error-prone application
    development/deployment. Smarty templates get compiled to PHP files
    internally (once), eliminating costly template file scans and
    leveraging the speed of PHP op-code accelerators.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Nothing in PHP is going to be faster than PHP itself with a proper byte code cache like &lt;a href=&quot;http://us3.php.net/apc&quot;&gt;APC&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;&amp;#8230;&lt;/h3&gt;

&lt;p&gt;So if Smarty isn&amp;#8217;t the right tool for the job, what is?  There are alternatives that do everything Smarty does, only better.  Our favorite PHP framework at the moment is &lt;a href=&quot;http://framework.zend.com/&quot;&gt;Zend Framework&lt;/a&gt;.  It supports caching, configuration, filtering, view helpers, and more.  It&amp;#8217;s also thoroughly documented, object-oriented, and unit tested.  There are other alternatives, as well.  &lt;a href=&quot;http://www.symfony-project.org/&quot;&gt;Symfony&lt;/a&gt;, for example, might be more your speed.  Or &lt;a href=&quot;http://phptal.motion-twin.com/&quot;&gt;PHPTAL&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But please&amp;#8211;no Smarty.&lt;/p&gt;</description>
          <pubDate>Fri, 08 Aug 2008 08:08:00 GMT</pubDate>
          <guid>http://www.nosmarty.net/articles/2008/08/08/is-smarty-right-for-me/</guid>
          <link>http://www.nosmarty.net/articles/2008/08/08/is-smarty-right-for-me/</link>
        </item>
    
    
  </channel>
</rss>
