If You're Smart, Don't Use Smarty

  • Today in the world of web frameworks people are looking for the perfect marriage of technology to scale their applications & provide rapid development. Through this time of transition some really great advancements have come out such as CodeIgniter, Ruby on Rails, & jQuery. Unfortunately, some of the things that are popular now seem to be good but can actually hurt your application. I'm talking about template engines in PHP.

    Why They Are Bad

    Template languages like Smarty are essentially another programming language built on top of PHP. This is a problem because php is what we call an interpreted language which means every time you load the page it is parsed on the fly and is outputted. When you add a template language on top of this the template language has to be interpreted, converted to php and then interpreted as php and outputted. This doubles the amount of work that the cpu has to do to return the page the user requested.

    At one point in an application I've been working on, I noticed that smarty was accounting for 60% of page load time. The page load time was still under a second but thats a huge amount of the process just for smarty to convert to php.

    What About Designers!?

    Using a template language just because you are working with designers is a poor excuse. Those designers could just as easily learn PHP Shorthand as they could something link Smarty. By teaching your designers small amounts of PHP, your giving them the knowledge to help with other areas of your application without having to learn something new.

    When It's Appropriate

    Sometimes a template language is the perfect solution. For instance if you were creating a CMS application where your users would have to edit template code you might want to use a template language. It would allow you to restrict which functions could be executed on the views side so that your application would be more secure. This is ok for CMS type apps like Wordpress and Expression Engine, but its usually something you dont need when your app is just something that your company will be editing. For use with codeigniter views, I have my own template library built and extended off of the initial work of Phil Sturgeon. You can take a look at his library here http://github.com/philsturgeon/codeigniter-template and I may release mine in the future.