PHP templating systems - pros and cons

Filed under: Software and Technology by Hari
Posted at 10:42 IST (last updated: 16 Jul 2008 @ 20:38 IST)
As I was in the middle of programming my new blogging system a few days ago (the PHP/SQLite one), I decided to go with Smarty, a PHP templating system which allows programmers to separate content presentation from the application logic. In its simplest form, a templating system provides a mechanism which allows you to define "tags" within HTML files which can be replaced by data generated by PHP code. But Smarty goes way beyond that and allows you to manipulate complex documents with relative ease while keeping the PHP functions separate from HTML code. Here are some of the pros and cons of using a PHP templating system. Keep in mind that using a system like Smarty is a highly addictive way of developing applications because it makes things so simple from a programming perspective and makes your code highly readable. I suspect though that it does have a few drawbacks as well. Here is my opinion on using a templating system as opposed to intermingling PHP tags with HTML.

Pros:
  • Makes application logic much easier to follow and PHP code much more easier to read.
  • Allows you to "skin" your website by using multiple templates. Thus the way your data is displayed is completely in the hands of designers who need no knowledge of PHP code to change the appearance of your application.
  • Provides a shorter development cycle. It also allows you to plan exactly how your data should be extracted from source. It makes visualization of your application much easier by providing you quick, alternative views of presenting your data.
  • Obviously it allows separation of skills as I mentioned above. A team of developers can be clearly separated into the "programmers" - the ones in charge of the PHP code, and the "designers" - the ones who actually lay out the presentation of the data.
  • Using a third-party templating system is a good way to avoid "reinventing" the wheel, thus speeding up development time considerably. It also ensures that you need have no worries about maintaining the template library yourself.

Cons:

Yes, there are some cons of using templating systems as well. Here's what you have to watch out for:

  • Processing of templates is an additional overhead you might wish to avoid. In spite of "fast" template parsers around (and Smarty is one of them) they still process text files in the ultimate analysis and any extra file processing increases execution time marginally.
  • Sometimes you encounter situations where the application logic can simply not be separated from the content presentation. Yes, this does happen in practical development although finding examples theoritically is tough.
  • Template designing can become extremely time-consuming because you are tempted to constantly refine and tweak the content presentation (because it's so easy to). As a result, if you are an individual programmer, you will find your priorities constantly shifting between coding and template development. This can be wearisome, since it involves working with two sets of files and you constantly need to co-ordinate your data fed by the PHP code with the template code.
  • Using a templating system in the middle of a project forces you to rewrite or even discard large chunks of your existing PHP code. Even if you strip out all the HTML from PHP, you would need to write a set of "feeder" functions which would allow you to submit the data to the templating system in a format that it can understand. (currently I am forced to consider doing this, since the admin panel of my blogging system is completely written in the old style - PHP intermingled with XHTML).

That's all. Whether you use a templating system or not really depends on how you visualize the development of your application. Monolithic or small projects can get away with intermingling PHP with HTML code, but even with a moderately sized PHP application, you would be forced to think of ways to minimize PHP embedded in the HTML files and so you would have to write your own library of "feeder" functions which insert data into HTML. So why not use a pre-existing library which not only saves you that task right off the bat, but allows gives you a lot more features and flexibility to work with?

So remember, if you are ever planning to develop a non-trivial PHP application involving a bit more than simple content display, make sure you have a plan in hand to separate the content display from the application logic. It will save you a lot of headache later.

8 comment(s)

Leave a comment »
  1. Templating systems like Smarty are great for large applications. Tiny applications that are little more than snippets won't benefit from them at all as the overhead would be greater than the code being supported.

    Great analysis, Hari. Intermingling HTML and PHP isn't a good thing if it can be avoided.

    Comment by (visitor) on 2 Dec 2007 @ 21:02 IST #
  2. Thanks RT. Yes, besides if you keep HTML and PHP mixed together and your project consists of more than one PHP file, you would find yourself either with a huge monolithic HTML/PHP file or have to copy/paste large sections of the code across files, leading to a messy, unmanageable situation

    Comment by hari (blog owner) on 3 Dec 2007 @ 07:14 IST #
  3. Great post, Hari. It's inspiring me to get back in to programming, also it is covering topics that are relevant to me. Thank you.

    Comment by titanium (visitor) on 8 Dec 2007 @ 01:06 IST #
  4. No problem titanium. I really like the Smarty templating system, but it does introduce a few inefficiencies which increase execution time.

    By the way, my blogging system is starting to take shape now.

    Comment by hari (blog owner) on 8 Dec 2007 @ 08:55 IST #
  5. This post is what Im looking for since Im planning to convert some of my website to Smarty. Thanks

    Comment by Mike (visitor) on 25 Sep 2008 @ 14:03 IST #
  6. Mike, no problem. Always a pleasure to know that my posts have been helpful. :>>

    Comment by hari (blog owner) on 25 Sep 2008 @ 19:40 IST #
  7. Hi Hari,

    Just want to ask if by looking at the source (view > source) you can determine if a website was created using Smarty? How? Thanks

    Comment by Chris (visitor) on 26 Sep 2008 @ 09:03 IST #
  8. Chris, I have no idea specifically for Smarty. But theoritically it shouldn't be possible to find out at all because all template processing is done on the server side and it's completely invisible to the client.

    The client (browser) gets only the processed HTML so it is not possible to determine how the site was created unless the website author puts up a notice or a link at the footer indicating the tools he's used to create the site.

    Comment by hari (blog owner) on 26 Sep 2008 @ 09:12 IST #

Leave a comment

First-time comments on this blog are moderated.
Your name*
Email ID*
(wont' be published)
Website
Your comments*
(No HTML allowed)
:-) :-D :biggrin: :-P ;-) 8-) :-( :mad: |-| :oops: :-/ :-| :roll:
bold italic quote code
Code* captcha Enter the code you see in the image
* required fields