PHP templating systems - pros and cons

Posted at 10:42:34 am by hari under Software and Technology (1492 views)

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.

4 comments

GravatarComment from: RT Cunningham [Member]
*****
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.
Sunday Dec 2, 2007 @ 21:02
GravatarComment from: hari [Member] · http://literaryforums.org
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
Monday Dec 3, 2007 @ 07:14
GravatarComment from: titanium [Visitor] · http://www.creativehedgehog.com
*****
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.
Saturday Dec 8, 2007 @ 01:06
GravatarComment from: hari [Member] · http://literaryforums.org
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.
Saturday Dec 8, 2007 @ 08:55

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
PoorExcellent
:!: :?: :idea: :) :D :p B) ;) :> :roll: :oops: :| :-/ :( :'( |-| :>> :yes: ;D :P :)) 88| :. :no: XX( :lalala: :crazy: >:XX
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)