{"id":463,"date":"2008-09-14T09:15:18","date_gmt":"2008-09-14T09:15:18","guid":{"rendered":"https:\/\/virtualchaos.co.uk\/blog\/2008\/09\/07\/to-smarty-or-not-to-smarty\/"},"modified":"2008-09-14T09:16:42","modified_gmt":"2008-09-14T09:16:42","slug":"to-smarty-or-not-to-smarty","status":"publish","type":"post","link":"https:\/\/virtualchaos.co.uk\/blog\/2008\/09\/14\/to-smarty-or-not-to-smarty\/","title":{"rendered":"To Smarty, or not to Smarty?"},"content":{"rendered":"<p>At <a href=\"http:\/\/www.talis.com\/\">Talis<\/a> we have been doing more than just our fair share of PHP development, in fact we are using the language to implement a number of new prototypes, products and services. One of the questions that I&#8217;ve been struggling with recently is whether or not we should be using the <a href=\"http:\/\/www.smarty.net\/\">Smarty template engine<\/a>. Whilst we have used it with a measure of success on several projects, I&#8217;ve been struggling with what Smarty represents. Smarty is written in PHP and allows you to create templates that are marked up using Smarty&#8217;s own language syntax however this means  that generated pages are precompiled at runtime down to pure PHP before being served. What strikes me is that Smarty is a Template Engine written in a language that was designed primarily for templating. So the question I&#8217;m left struggling with is: <em>Why do you want to add the overhead of a secondary language to do what your first language already does and is fully capable of doing?<\/em><\/p>\n<p>The more I think about it, the more I&#8217;m convinced that the answer is that you dont! I&#8217;m going to try to explain why by examining some of the reasons that are often cited for using Smarty.<\/p>\n<p><strong>Separating PHP from HTML<\/strong><\/p>\n<p>I&#8217;ve often heard his mantra, but I would describe it in a slightly different way and that&#8217;s by saying that what&#8217;s actually desired is to keep keep your &#8220;business logic&#8221; separate from your &#8220;presentation logic&#8221;, which is subtly different. If you accept that the two are indeed different then you should realise that all that\u2019s actually required is a way to keep your Views and Controllers separated which can be achieved using plain PHP in your view scripts (templates), without the overhead of having to add a new language into the mix.<\/p>\n<p><strong>Easier to read<\/strong><\/p>\n<p>I&#8217;ve always struggled with this and here&#8217;s why &#8230; does anyone really think that this <\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>&#8230;is really easier to read than its pure PHP equivelant? here &#8230; <\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>&#8230; I honestly don&#8217;t think that it is. I know that there&#8217;s a view in the community that there are front-end only Developers out there or Web Designers that shouldn&#8217;t have to learn PHP, but are they really better off being taught a different language? Firstly I&#8217;d question if there are actually Developers out there who know Smarty and don&#8217;t have an understanding of PHP, but for the purposes of this exercise lets assume that these mythical smarty only developers do exist then I have to confess I agree entirely with Harry Feuks<sup><a href=\"#footnote_0_463\" id=\"identifier_0_463\" class=\"footnote-link footnote-identifier-link\" title=\"phpPatterns, http:\/\/www.phppatterns.com\/docs\/design\/templates_and_template_engines\">1<\/a><\/sup> when he says<\/p>\n<blockquote>\n<pre>Walk them through a few sections of the PHP Language Reference\r\nand show them how PHP\u2018s control structures and simple functions like\r\necho() works and that\u2019s all they need.<\/pre>\n<\/blockquote>\n<p> and also with Brian Lozier<sup><a href=\"#footnote_1_463\" id=\"identifier_1_463\" class=\"footnote-link footnote-identifier-link\" title=\"Template Engines, http:\/\/www.massassi.com\/php\/articles\/template_engines\/\">2<\/a><\/sup> who describes it much more convincingly when he says:<\/p>\n<blockquote>\n<pre>\r\nBasically, it just provides an interface to PHP with new syntax. When \r\nstated like that, it seems sort of silly. Is it actually more simple to \r\nwrite {foreach --args} than <? foreach --args ?>? If you do think it's\r\nsimpler, consider this. Is it so much simpler that there is value in \r\nincluding a huge template library to get that separation? Granted, \r\nSmarty offers many other great features (caching, for instance), but it \r\nseems like the same benefits could be gained without the huge \r\noverhead of including the Smarty class libraries.<\/pre>\n<\/blockquote>\n<p><strong>Speed<\/strong><\/p>\n<p>I&#8217;ve read several articles online that try to explain that there is a performance overhead when including Smarty. Yes I know you can cache your Smarty templates to improve performance in fact it&#8217;s not a question of &#8216;can&#8217; it&#8217;s a question of &#8216;you really have to&#8217; as Harry points out in the same article<\/p>\n<blockquote>\n<pre>if you hadn\u2019t chosen to write your own programming language\r\nand built an interpreter with PHP to parse it, you wouldn\u2019t have slowed\r\ndown your application in the first place!<\/pre>\n<\/blockquote>\n<p>I performed a little test to try and illustrate what happens when you simple include Smarty, let alone actually use it. First off create yourselves a file called test.php and add the following line of code to it:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">span class=&#8221;st0&#8243;>&quot;Hello world&quot;<\/span<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>Now let&#8217;s benchmark this using Apache Benchmark, thanks to Vivek  for providing some useful documentation on how to do this<sup><a href=\"#footnote_2_463\" id=\"identifier_2_463\" class=\"footnote-link footnote-identifier-link\" title=\"How to performance benchmark a web server, http:\/\/www.cyberciti.biz\/tips\/howto-performance-benchmarks-a-web-server.html\">3<\/a><\/sup>. Open up a terminal window and issue the following command:<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>Which on my system ( mac osx 10.5.2 4gb ram php5+apache2 ) resulted in the following:<\/p>\n<blockquote>\n<pre>\r\nnadeemshabir$ ab -c 10 -n 100 http:\/\/localhost\/test.php\r\nThis is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0\r\nCopyright 1996 Adam Twiss, Zeus Technology Ltd, http:\/\/www.zeustech.net\/\r\nCopyright 2006 The Apache Software Foundation, http:\/\/www.apache.org\/\r\n\r\nBenchmarking localhost (be patient).....done\r\n\r\n\r\nServer Software:        Apache\/2.2.9\r\nServer Hostname:        localhost\r\nServer Port:            80\r\n\r\nDocument Path:          \/test.php\r\nDocument Length:        11 bytes\r\n\r\nConcurrency Level:      10\r\nTime taken for tests:   0.58753 seconds\r\nComplete requests:      100\r\nFailed requests:        0\r\nWrite errors:           0\r\nTotal transferred:      19695 bytes\r\nHTML transferred:       1111 bytes\r\nRequests per second:    1702.04 [#\/sec] (mean)\r\nTime per request:       5.875 [ms] (mean)\r\nTime per request:       0.588 [ms] (mean, across all concurrent requests)\r\nTransfer rate:          323.39 [Kbytes\/sec] received\r\n\r\nConnection Times (ms)\r\n              min  mean[+\/-sd] median   max\r\nConnect:        0    0   0.5      0       2\r\nProcessing:     2    4   5.4      4      34\r\nWaiting:        1    4   5.5      3      33\r\nTotal:          2    5   5.3      4      34\r\n\r\nPercentage of the requests served within a certain time (ms)\r\n  50%      4\r\n  66%      4\r\n  75%      5\r\n  80%      6\r\n  90%      8\r\n  95%     14\r\n  98%     27\r\n  99%     34\r\n 100%     34 (longest request)\r\nNadeems-Computer:smarty-test nadeemshabir$\r\n<\/pre>\n<\/blockquote>\n<p>So the important figures are Request Per Second: <strong>1702.04<\/strong>, Time per request: <strong>5.875 [ms]<\/strong> (mean),  Time per request: <strong>0.588 [ms]<\/strong> (mean, across all concurrent requests) and Transfer rate: <strong>323.39 [Kbytes\/sec]<\/strong> received. Now let&#8217;s create a new test file called test-smarty.php, and add to it the following:\n<\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">span class=&#8221;st0&#8243;>&#8217;smarty\/libs\/Smarty.class.php&#8217;<\/span><span class=\"st0\">&quot;Hello world&quot;<\/span<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>and run the same test again &#8230; <\/p>\n<div class=\"dean_ch\" style=\"white-space: wrap;\">\n<ol>\n<li class=\"li1\">\n<div class=\"de1\">&nbsp;<\/div>\n<\/li>\n<\/ol>\n<\/div>\n<p>which results in the following:<\/p>\n<blockquote>\n<pre>\r\nnadeemshabir$ ab -c 10 -n 100 http:\/\/localhost\/test-smarty.php\r\nThis is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0\r\nCopyright 1996 Adam Twiss, Zeus Technology Ltd, http:\/\/www.zeustech.net\/\r\nCopyright 2006 The Apache Software Foundation, http:\/\/www.apache.org\/\r\n\r\nBenchmarking localhost (be patient).....done\r\n\r\n\r\nServer Software:        Apache\/2.2.9\r\nServer Hostname:        localhost\r\nServer Port:            80\r\n\r\nDocument Path:          \/test-smarty.php\r\nDocument Length:        11 bytes\r\n\r\nConcurrency Level:      10\r\nTime taken for tests:   0.310262 seconds\r\nComplete requests:      100\r\nFailed requests:        0\r\nWrite errors:           0\r\nTotal transferred:      19500 bytes\r\nHTML transferred:       1100 bytes\r\nRequests per second:    322.31 [#\/sec] (mean)\r\nTime per request:       31.026 [ms] (mean)\r\nTime per request:       3.103 [ms] (mean, across all concurrent requests)\r\nTransfer rate:          61.24 [Kbytes\/sec] received\r\n\r\nConnection Times (ms)\r\n              min  mean[+\/-sd] median   max\r\nConnect:        0    2   4.7      0      17\r\nProcessing:     9   26  14.3     22      84\r\nWaiting:        8   25  14.1     22      82\r\nTotal:         13   28  14.7     24      87\r\n\r\nPercentage of the requests served within a certain time (ms)\r\n  50%     24\r\n  66%     28\r\n  75%     35\r\n  80%     37\r\n  90%     46\r\n  95%     70\r\n  98%     84\r\n  99%     87\r\n 100%     87 (longest request)\r\nNadeems-Computer:smarty-test nadeemshabir$\r\n<\/pre>\n<\/blockquote>\n<p>\nNow we can see a dramatic difference those important figures are now &#8211;  Request Per Second: <strong>322.31<\/strong>, Time per request: <strong>31.026 [ms]<\/strong> (mean),  Time per request: <strong>3.103 [ms]<\/strong> (mean, across all concurrent requests) and Transfer rate: <strong> 61.24[Kbytes\/sec]<\/strong> received. If you take simply the requests per second we are now only serving almost a sixth of the requests we originally served, <strong>and that&#8217;s without even using Smarty, that&#8217;s from just simply including the codeline!<\/strong>\n<\/p>\n<p><strong>Conclusions<\/strong><\/p>\n<p>To my mind those results and the arguments I&#8217;ve cited are pretty emphatic, I can&#8217;t justify using a template engine that adds so much overhead when you can achieve the same results using a pure PHP implementation. This view seems to be widely held and accepted, it&#8217;s part of the reason why many other Template Engines like <a href=\"http:\/\/phpsavant.com\/\">Savant<\/a>, <a href=\"http:\/\/framework.zend.com\/manual\/en\/zend.view.html\">Zend_View<\/a> and <a href=\"http:\/\/solarphp.com\/class\/Solar_View\">Solar_View<\/a> all embrace a different ethos to Smarty i.e they don&#8217;t compile your templates into PHP because they use PHP as the template language.<\/p>\n<p>If you are at all unconvinced by the arguments I&#8217;ve presented then like me you might want to consider the words of <a href=\"http:\/\/hasin.wordpress.com\/\">Hasin Hayder<\/a> who is the author of the popular book <a href=\"http:\/\/www.packtpub.com\/smarty\/book\/mid\/160606wf4ulr\">Smarty PHP Template Programming and Applications<\/a> which I own a copy of :). He also created this <a href=\"http:\/\/hasin.wordpress.com\/2006\/06\/10\/smarty-cheat-sheet-version-20\/\">Smarty Cheat Sheet<\/a> which I along with several of my colleagues have copies of on our desks. Earlier this year Hasin wrote an article entitled <a href=\"http:\/\/hasin.wordpress.com\/2008\/01\/05\/once-upon-a-time-there-was-smarty\/\">Once upon a time there was Smarty<\/a>, in this article Hasin touches on many of the points that I and others have made, and whilst the depth of my knowledge of smarty could easily be challenged Hasin is without question an expert, so he when he said &#8230;<\/p>\n<blockquote>\n<pre>\r\nI seriously don\u2019t think there is need of Smarty anymore. Its dead! If \r\nyou guys don\u2019t agree with me, you can spend hell lot of time learning \r\nthat {$name} actually does what you could do with \u201cecho $name\u201d. If \r\nyou write a dispatcher which is smart enough to bring the variables \r\nfrom a controller scope to the scope of a view, why do u need to learn \r\na separate parser like smarty? ... Learning all those functions, loops, \r\nlogics, caching and others in smarty takes so much time that I would \r\nlove to suggest you to learn writing efficient PHP code in template layer \r\nrather than learning something like smarty ...<\/pre>\n<\/blockquote>\n<p> &#8230; I stopped and listened &#8230; and then I found myself agreeing with his closing statement &#8230; <\/p>\n<blockquote>\n<pre>Sometime it\u2019s better to realize thats its time to unlearn something.<\/pre>\n<\/blockquote>\n<ol class=\"footnotes\"><li id=\"footnote_0_463\" class=\"footnote\">phpPatterns, <a href=\"http:\/\/www.phppatterns.com\/docs\/design\/templates_and_template_engines\">http:\/\/www.phppatterns.com\/docs\/design\/templates_and_template_engines<\/a>[<a href=\"#identifier_0_463\" class=\"footnote-link footnote-back-link\">back<\/a>]<\/li><li id=\"footnote_1_463\" class=\"footnote\">Template Engines, <a href=\"http:\/\/www.massassi.com\/php\/articles\/template_engines\/\">http:\/\/www.massassi.com\/php\/articles\/template_engines\/<\/a>[<a href=\"#identifier_1_463\" class=\"footnote-link footnote-back-link\">back<\/a>]<\/li><li id=\"footnote_2_463\" class=\"footnote\">How to performance benchmark a web server, <a href=\"http:\/\/www.cyberciti.biz\/tips\/howto-performance-benchmarks-a-web-server.html\">http:\/\/www.cyberciti.biz\/tips\/howto-performance-benchmarks-a-web-server.html<\/a>[<a href=\"#identifier_2_463\" class=\"footnote-link footnote-back-link\">back<\/a>]<\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>At Talis we have been doing more than just our fair share of PHP development, in fact we are using the language to implement a number of new prototypes, products and services. One of the questions that I&#8217;ve been struggling &hellip; <a href=\"https:\/\/virtualchaos.co.uk\/blog\/2008\/09\/14\/to-smarty-or-not-to-smarty\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[124,399],"class_list":["post-463","post","type-post","status-publish","format-standard","hentry","category-development","tag-php","tag-talis"],"_links":{"self":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts\/463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=463"}],"version-history":[{"count":0,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/posts\/463\/revisions"}],"wp:attachment":[{"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/virtualchaos.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}