Web Development Software
5/15/04
<< 72 Squared Weblog

Is SMARTY redundant?

Smarty is a templating system. It allows developers to separate presentation logic from business logic. I've heard it argued that PHP allows the developer to separate presentation from business logic already, so why bother with using a templating system on top of that? Doesn't that just add a bloated class library on top of your project that slows down your page load time and decreases server performance?

I disagree. Smarty and other template systems had it right. So many PHP developers write spaghetti code. That means, they mix their html and php all together and hope for the best. As sites grow more and more complex, this approach becomes completely unworkable. The solution? Templates!
There has been a lot of discussion in PHP forums and chat rooms about template systems. Template systems allow designers to create clean html layouts with placeholders for dynamic content. The programmer feeds the content into the template through the template object. Viola! Beautiful pages that are easy to maintain.
Smarty Templates are far in the lead, and with good reason. It is feature rich with all the presentation logic necessary to build a page, allowing programmers to easily separate business logic from the presentation layer. Of course, adding a templating system adds an extra layer on top of the PHP and programmers often worry that the template object will slow performance and scaleability. It's true, a template will slow your page load time down a bit.
Personally, it's a sacrifice I'm willing to make. You can gain that time back with well written code elsewhere in your application. I found that as a percentage of page load time, presentation logic took up only a very small percentage of memory and time. The biggest offender is often Database connections, abstractions and caching of data. When I switched to a lean database abstraction my application performance improved dramatically even with Smarty.
The benefits compound upon each other. A site written with a template system makes it easier for designers to create the presentation layer. Programmers can make changes to the business logic without breaking the layout. And it makes the code SO MUCH EASIER TO READ! I started using Smarty only about 6 months ago and saw immediate results. I found I was developing faster, designing more efficiently, and completing my work in record time. The mojavi framework points out that PHP is a template language, so why bother adding an extra layer? Just show a little discipline and separate presentation logic from business logic and use PHP as a templating language. There is a good point to that and if I hadn't already started using Smarty, I would have probably agreed with this philosophy. The problem is that Smarty just makes it SO EASY to do complicated presentation tasks. Things like truncating a string to the nearest word, or placing an array inside the cells of a table.
Once I started using smarty I was hooked. Dropdown menus are simple to create dynamically. Another favorite feature is to give a parameter a default value if none is specified. In smarty, you write :{$variable|default:'testing']}. PHP you write: Not a big deal, right? But as you add more and more presentation parameters it becomes more and more complex to control in PHP. What if you want to truncate to the nearest word as well? In Smarty, I just write {$variable|default:'testing'|truncate:150} in PHP I have to manipulate the variable after I detect if it is actually set. While it is not that difficult to write the code to do this, when reading the layout, the Smarty code can be deciphered in a glance. Not so wiith PHP. I'm sure you can accomplish all the same things without the Template Object, but, it is so much easier with a template, so . . . Mojavi was correct in building a framework that did not rely on any one template framework. They build a very simple template object that can be extended to rely on a template system of choice : Smarty, TAL, SmartTemplate, and many more. Or just use php.
I chose Smarty because it's easy to use, but perhaps that's just personal preference. The important thing is to use some system to separate business logic from presentation logic. Once you do, you won't know how you ever built a site before!

See: http://smarty.php.net