Zend Framework - Smarty or not to Smarty
I have immersed myself in the Zend Framework for the past 30 days; spending that time learning the ins-and-outs. I’ve started writing my first application utilizing the framework, and must say so far I am pretty happy with it’s flexibility. The learning curve is a bit higher than I had expected, but I attribute this to the vast amount of complexity involved in trying to make the application flexible.
In the development of the front end of my application I found myself at dilemma to Smarty or not to Smarty (or a derivative there of). I started researching the history of Smarty and other types of similar formated coding standards if you will, to better understand the real concept for using such a format. The classic and consistent argument is separation of application code from display code, in this instance the separation of php from html code. Which only brought forth further questions; from my perspective with a properly designed MVC application utilizing models to translate object code to visual representations, Smarty did not appear to be needed.
A quick and rough example. Your application returns you an array of data; lets say
$data = array('firstname'=>'nick', 'lastname' => 'white');
If I wanted to display the name in a table within my html, traditionally you would begin your html table tags, foreach loop through the array adding your table data tags as needed and end your php code and respectively your table.
Using what I consider to be a properly formated MVC environment, I would apply a data model within the action controller of my application, formatting the “mynametable” prior to the rendering layer and assigning the model to a variable. Using this layout within my application allows me to need no more than the standard echo variablename to display the table and it’s data.
So I guess my question is, if you utilize your model’s within your MVC environment to handle display data structure, do you really need a Smarty system to display the data….
Leave comments… I want to see your thoughts.
31 May 2008 vMonkey
One Response to “Zend Framework - Smarty or not to Smarty”
Leave a Reply
You must be logged in to post a comment.
Short answer
By properly designing a project as you’ve described, templating systems like Smarty are nothing but needless overhead.
Long answer
Each time I have to work on something that has been written using Smarty it hurts me deep inside. I figured I was just biased because I wasn’t that familiar with it and I was coming into the middle of whatever I was working on so I tried something from scratch.
It was awful.
It took me a while to figure out why and I realized that it was because I was, effectively, having to learn an entire new language just to get stuff to display. The entire time I was just aching to use a simple echo. Ultimately, that’s all Smarty is doing, it’s just wrapping it up in this whole extra layer to do it.
My only thought as to why this even exists is so that designers or non-PHP programmers can make Smarty templates that can be attached to PHP back ends. I don’t think that learning Smarty is really that much easier than learning PHP. I guess in the end it comes down to my concept which is that, if you look at it right, PHP is, at its core, a templating system.
Obviously that’s a simplistic view but I don’t think an unrealistic one and I don’t mean to take away from PHP’s real power. Look at it a different way and it is much, much more than just a templating system but that’s what I like about PHP. The flexibility if affords you in design and execution of a program.
I revisit this concept on occasion and ask all the various programmers I work with about it and I have yet to have a Smarty fanatic tell me a single good reason why it’s worth it.
Until that day comes, Smarty can bite me.