Hari's Corner

Humour, comics, tech, law, software, reviews, essays, articles and HOWTOs intermingled with random philosophy now and then

PHP templating systems - pros and cons

Filed under: Software and Technology by Hari
Posted on Sat, Dec 1, 2007 at 10:42 IST (last updated: Wed, Jul 16, 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:

Cons:

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

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)

  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 Sun, Dec 2, 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 Mon, Dec 3, 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 Sat, Dec 8, 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 Sat, Dec 8, 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 Thu, Sep 25, 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 Thu, Sep 25, 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 Fri, Sep 26, 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 Fri, Sep 26, 2008 @ 09:12 IST #

Comments closed

The blog owner has closed further commenting on this entry.