To Smarty, or not to Smarty?

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’ve been struggling with recently is whether or not we should be using the Smarty template engine. Whilst we have used it with a measure of success on several projects, I’ve been struggling with what Smarty represents. Smarty is written in PHP and allows you to create templates that are marked up using Smarty’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’m left struggling with is: 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?

The more I think about it, the more I’m convinced that the answer is that you dont! I’m going to try to explain why by examining some of the reasons that are often cited for using Smarty.

Separating PHP from HTML

I’ve often heard his mantra, but I would describe it in a slightly different way and that’s by saying that what’s actually desired is to keep keep your “business logic” separate from your “presentation logic”, which is subtly different. If you accept that the two are indeed different then you should realise that all that’s 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.

Easier to read

I’ve always struggled with this and here’s why … does anyone really think that this

  1.  

…is really easier to read than its pure PHP equivelant? here …

  1.  

… I honestly don’t think that it is. I know that there’s a view in the community that there are front-end only Developers out there or Web Designers that shouldn’t have to learn PHP, but are they really better off being taught a different language? Firstly I’d question if there are actually Developers out there who know Smarty and don’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 Feuks1 when he says

Walk them through a few sections of the PHP Language Reference
and show them how PHP‘s control structures and simple functions like
echo() works and that’s all they need.

and also with Brian Lozier2 who describes it much more convincingly when he says:

Basically, it just provides an interface to PHP with new syntax. When 
stated like that, it seems sort of silly. Is it actually more simple to 
write {foreach --args} than ? If you do think it's
simpler, consider this. Is it so much simpler that there is value in 
including a huge template library to get that separation? Granted, 
Smarty offers many other great features (caching, for instance), but it 
seems like the same benefits could be gained without the huge 
overhead of including the Smarty class libraries.

Speed

I’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’s not a question of ‘can’ it’s a question of ‘you really have to’ as Harry points out in the same article

if you hadn’t chosen to write your own programming language
and built an interpreter with PHP to parse it, you wouldn’t have slowed
down your application in the first place!

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:

  1. span class=”st0″>"Hello world"

Now let’s benchmark this using Apache Benchmark, thanks to Vivek for providing some useful documentation on how to do this3. Open up a terminal window and issue the following command:

  1.  

Which on my system ( mac osx 10.5.2 4gb ram php5+apache2 ) resulted in the following:

nadeemshabir$ ab -c 10 -n 100 http://localhost/test.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        Apache/2.2.9
Server Hostname:        localhost
Server Port:            80

Document Path:          /test.php
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.58753 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      19695 bytes
HTML transferred:       1111 bytes
Requests per second:    1702.04 [#/sec] (mean)
Time per request:       5.875 [ms] (mean)
Time per request:       0.588 [ms] (mean, across all concurrent requests)
Transfer rate:          323.39 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       2
Processing:     2    4   5.4      4      34
Waiting:        1    4   5.5      3      33
Total:          2    5   5.3      4      34

Percentage of the requests served within a certain time (ms)
  50%      4
  66%      4
  75%      5
  80%      6
  90%      8
  95%     14
  98%     27
  99%     34
 100%     34 (longest request)
Nadeems-Computer:smarty-test nadeemshabir$

So the important figures are Request Per Second: 1702.04, Time per request: 5.875 [ms] (mean), Time per request: 0.588 [ms] (mean, across all concurrent requests) and Transfer rate: 323.39 [Kbytes/sec] received. Now let’s create a new test file called test-smarty.php, and add to it the following:

  1. span class=”st0″>’smarty/libs/Smarty.class.php’"Hello world"

and run the same test again …

  1.  

which results in the following:

nadeemshabir$ ab -c 10 -n 100 http://localhost/test-smarty.php
This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient).....done


Server Software:        Apache/2.2.9
Server Hostname:        localhost
Server Port:            80

Document Path:          /test-smarty.php
Document Length:        11 bytes

Concurrency Level:      10
Time taken for tests:   0.310262 seconds
Complete requests:      100
Failed requests:        0
Write errors:           0
Total transferred:      19500 bytes
HTML transferred:       1100 bytes
Requests per second:    322.31 [#/sec] (mean)
Time per request:       31.026 [ms] (mean)
Time per request:       3.103 [ms] (mean, across all concurrent requests)
Transfer rate:          61.24 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   4.7      0      17
Processing:     9   26  14.3     22      84
Waiting:        8   25  14.1     22      82
Total:         13   28  14.7     24      87

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     28
  75%     35
  80%     37
  90%     46
  95%     70
  98%     84
  99%     87
 100%     87 (longest request)
Nadeems-Computer:smarty-test nadeemshabir$

Now we can see a dramatic difference those important figures are now – Request Per Second: 322.31, Time per request: 31.026 [ms] (mean), Time per request: 3.103 [ms] (mean, across all concurrent requests) and Transfer rate: 61.24[Kbytes/sec] received. If you take simply the requests per second we are now only serving almost a sixth of the requests we originally served, and that’s without even using Smarty, that’s from just simply including the codeline!

Conclusions

To my mind those results and the arguments I’ve cited are pretty emphatic, I can’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’s part of the reason why many other Template Engines like Savant, Zend_View and Solar_View all embrace a different ethos to Smarty i.e they don’t compile your templates into PHP because they use PHP as the template language.

If you are at all unconvinced by the arguments I’ve presented then like me you might want to consider the words of Hasin Hayder who is the author of the popular book Smarty PHP Template Programming and Applications which I own a copy of :). He also created this Smarty Cheat Sheet which I along with several of my colleagues have copies of on our desks. Earlier this year Hasin wrote an article entitled Once upon a time there was Smarty, 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 …

I seriously don’t think there is need of Smarty anymore. Its dead! If 
you guys don’t agree with me, you can spend hell lot of time learning 
that {$name} actually does what you could do with “echo $name”. If 
you write a dispatcher which is smart enough to bring the variables 
from a controller scope to the scope of a view, why do u need to learn 
a separate parser like smarty? ... Learning all those functions, loops, 
logics, caching and others in smarty takes so much time that I would 
love to suggest you to learn writing efficient PHP code in template layer 
rather than learning something like smarty ...

… I stopped and listened … and then I found myself agreeing with his closing statement …

Sometime it’s better to realize thats its time to unlearn something.
  1. phpPatterns, http://www.phppatterns.com/docs/design/templates_and_template_engines[back]
  2. Template Engines, http://www.massassi.com/php/articles/template_engines/[back]
  3. How to performance benchmark a web server, http://www.cyberciti.biz/tips/howto-performance-benchmarks-a-web-server.html[back]

Curating the Dark Data in the long tail of science


ABSTRACT

There is a wealth of scientific data that is almost impossible to see. This is science’s dark data. Much of this data resides in the long tail of science or “small” data collection efforts. Instrumentation has made it possible to develop large collections of relatively homogeneous data, be it from space sensors or high throughput gene sequencers. The monolithic collections are easy to find and search. Dark data on the other hand may constitute the larger mass of scientific information. The collections that make up the dark data of science are much smaller but also much more numerous, being generated by thousands of scientists, on a much broader number of scientific questions, and in a complex array of formats. Unfortunately, it is also more prone to be overlooked and lost over time. Using new technology, the economics of the internet, and change in the sociology of science it is possible to make greater use of this data than was possible in the past. Data curators are the people who develop and use these technologies and procedures to make this data more useful, insuring a more efficient return on investment in the enterprise of science.

This is a really interesting tech talk given by P. Bryan Heidorn from the National Science Foundation Division of Biological Infrastructure and Associate Professor, University of Illinois.

I found the talk to be particularly useful, I’ve never come across the term Digital Curation before, and surprised to learn that it is defined as:

Digital curtaion is the acquisition, management, appraisal, and serving 
of data to maximise it's usefulness.

Curation embraces and goes beyond that of enhanced present day
re-use, and of archival responsibility, to embrace stewardship that adds
value through the provision of context and linkage: placing emphasis
on publishing data in ways that ease re-use and promoting accountability
and integration. (Rusbridge et. al, 2005)

What surprises me is that the goals of these curators are not too dissimilar to the goals of those of us working in the Linked Open Data movement, and I’m wondering whether these two communities should work more closely together … very interesting indeed.

OLPC the new XO-2 model

This video provides a quick look at the second generation OLPC, XO-2. The new system has two touch-sensitive displays. The XO-2 will be much smaller than the original machine , almost half the size! and will have a foldable e-book form factor. “The next generation laptop should be a book” according Nicholas Negropante.

The XO-2 will also have dual indoor-and-sunlight displays, which was pioneered by former OLPC CTO Mary Lou Jepsen. This new design will provide:

  • a right and left page in vertical format
  • a hinged laptop in horizontal format,
  • and a flat, two-screen continuous surface for use in tablet mode.

Younger children will be able to use simple keyboards to get going, and older children will be able to switch between keyboards customized for applications as well as for multiple languages

The have also stated that the new model will reduce power consumption down to 1 watt!

Find out more here.

Photosynth Goes Live :) but only on Windows :(

Microsoft have finally released the first public version of Photosynth, that meshes many photos of the same place together into a 3D Landscape. I have played with Photosynth in the past and have talked about it on this blog. It’s a hugely impressive technology and I can’t wait to use it in earnest. Sadly it’s only available on Windows at the moment, and sadly it wont even work on Windows running on VMWare! Hopefully they’ll launch cross platform versions of the software soon …

The Truth about Innovation – Max Mckeown

All the way back in May of this year Max sent me a preview copy of his upcoming book entitled the Truth about Innovation and asked if I’d read it and offer some feedback or write a review. I did read the book and thoroughly enjoyed it but didn’t get around to reviewing it due to a myriad of other commitments – so apologies Max :).

The other day I was watching a documentary about Pixar much of which focused on the culture of Innovation at Pixar. I immediately recalled some things that Max had written in his book, in fact many of the sentiments in the documentary were directly echoed by the observations Max had been making. Feeling somewhat abashed I re-read The Truth about Innovation over the last couple of days and think it’s about time I wrote a (pre)-review (the book is scheduled for release on September 12th according to Amazon).

The book is well written and well researched, Max makes great use of examples from industry to illustrate his fifty or so “Truths” about innovation. For me It’s a great book because it shows that innovation is about keeping our eyes and minds open, and constantly questioning what we know. Max has previously tried to sum up innovation as:

The term innovation may refer to both radical and incremental changes 
in thinking, in things, in processes or in services ...

I’ve always liked this definition, it feels practical without feeling overly idealistic. There was always a danger with this book that in presenting what he thought were his 50 truths about innovation he would fall into the trap of sounding overly idealistic or offering advice that was either vague or not particularly practical. Thankfully this is not the case. What he has written is short, sharp and extremely focused, yet tempered by an honest down to earth style that makes it not only easy to read but also easy to identify with.

Some of truths really hammered home for me because they resonated so much with what we are trying to do at Talis, not just in terms of the technologies we are building but also in terms of the culture we are trying to create and I completely agree with Max when he says in Chapter 31:

Culture is the sum total of the values, beliefs, assumptions, and traditions
of the organization. Culture is established at the time that the company is 
founded and it develops based on the experiences of the people in the 
organization. It is not the same as a neatly typed mission statement and 
cannot be transformed with half-hearted attempts or superficial 
declarations.

There are differences in character, rhythm, preferences, traditions, jokes,
discipline, and priorities between the most successfully innovative 
organizations and the rest. Turning great insights into practical solutions
is the result of what is done and the way it is done. Making the transition
to an innovation culture is difficult because it doesn’t depend on policies 
or processes in isolation.

Max also goes onto differentiate between cultures that encourage innovation and those that discourage innovation, thankfully for me Talis fits squarely in the first column but only because thats where we, as individuals, and as a group, want it to be:

Cultures that encourage innovation Cultures that discourage innovation
Emotionally connected Dispassionately disconnected
Power Sharing Power Hoarding
Visionary & Forward Thinking Tied to routine & past practise
Trusting with minimal rules Controlling and negative
Positive and highly principled Highly financially focused
People identify with leaders Remote managers issue edicts
Customer service obsessed Performance freaks
Thirst for listening and learning Excessive denial psychology
Valued people like the company Best people feel devalued
Decisions are based on merit Hierarchy slows progress

In chapter 25 Max talks about hiring people for how the learn and not what the know. This is critical in my view for any organisation and goes to the heart of innovation, but in my mind its not just about learning new things its about a passionate willingness to want to learn new things, as Tennyson says to strive, to seek, to find or as Max puts it:

Far more important than what a person knows is how the person learns.
What a person knows matters, you want experts, you want knowledge, 
but it should be taken as a given. If the person can’t do the basics then 
you shouldn’t hire them with the expectation that they can. You need 
enough people in the company who can make whatever it is that you 
are trying to sell. The way that people have learned what they know and 
the way they intend learning what they will need to know in the future is 
the difference between candidates. It’s also the difference between companies. 
Learning new things is at the heart of innovation.

I could easily go on for hours but I wont. This is a wonderful book full of practical advice and insights into how some of the most respected organisations in the world have succeeded and failed to be innovative. I thoroughly recommend reading this book.


Click here to buy from Amazon

I’m going to do something I wasn’t intending to originally and that’s to list Max’s fifty-ish truths, because it occurred to me that actually these chapter headings say more about the book and the message that Max is trying to communicate than any short review could:

  • Truth 01: Innovation is new stuff that is useful
  • Truth 02: A beautiful idea is never perfect
  • Truth 03: A crisis is a terrible thing to waste
  • Truth 04: A great innovation deserves a great name
  • Truth 05: A fool can do either, a genius does both
  • Truth 06: All new ideas are made of old ideas
  • Truth 07: Bet small to win big
  • Truth 08: Better to ask forgiveness than permission
  • Truth 09: Creativity is a process not an action
  • Truth 10: Creativity is its own reward
  • Truth 11: Crowds are mad, bad,and advantageous to know
  • Truth 12: Cut innovation some slack
  • Truth 13: Cure apathy by sharing purpose
  • Truth 14: Do what your competition wont
  • Truth 15: Don’t get lost in translation
  • Truth 16: Different structural strokes for different folks
  • Truth 17: Even useless can be useful
  • Truth 18: Every company needs an idea market
  • Truth 19: Everyone can learn to think better
  • Truth 20: Find the buzz that can work for your people
  • Truth 21: Free your children before someone eats them
  • Truth 22: Get your ducks in a row
  • Truth 23: Got to share to get more
  • Truth 24: Hell hath no fury like a talent spurned
  • Truth 25: Hire for how they learn, not what they know
  • Truth 26: How much is the future worth?
  • Truth 27: Ideas are fragile, handle with care
  • Truth 28: Innovate your way out of recession
  • Truth 29: Innovation can be measured
  • Truth 30: Innovation is not everywhere
  • Truth 31: It’s a cultural thing
  • Truth 32: Just enough disunity for progress
  • Truth 33: Leaders get the innovation they deserve
  • Truth 34: Little differences make a big difference
  • Truth 35: Look outside for a bigger brain
  • Truth 36: Madonna knows more than your boss
  • Truth 37: Meeting of minds not mindless meetings
  • Truth 38: Most things will fail, get over it
  • Truth 39: Not all networks are created equal
  • Truth 40: Open spaces, open minds
  • Truth 41: People judge your first, then your ideas
  • Truth 42: Power is originality’s best friend
  • Truth 43: Quick fixes can lead to great innovations
  • Truth 44: Reinventing the wheel is a good thing
  • Truth 45: Second can be better than first
  • Truth 46: Some ideas are easier to swallow
  • Truth 47: Sometimes you have to gamble everything
  • Truth 48: Success is an S-shaped curve
  • Truth 49: The ideal design is the simplest design
  • Truth 50: This is going to hurt
  • Truth 51: Understand change to make progress
  • Truth 52: Welcome to the innovation factory
  • Truth 53: What you know can hurt you
  • Truth 54: Who the hell cares where it was built
  • Truth 55: You can’t control waves so learn to surf!

… I’ve just discovered that the Preview of the Book is available to read on Scribd here for free.