Scredit Crunch

Why Template Toolkit sucks

Posted in Rants by setok on February 5, 2008

A lot of recent Scred development has been built around the Perl Template Toolkit system. I have always been of the opinion that mixing code with the final layout is a very bad idea and this position has only been strengthened by recent experiences. Templates should be just that, templates, or prototypes, of the final result. Files where the visual layout is tweaked, without caring about the logic in the background and which are then processed to get the final result. This does not require anything fancy, just placeholders for the actual data. Let a real script language do the job of managing the logic.

Here are some reasons why Template Toolkit’s model is fundamentally wrong:

  • Logic and representation can get mixed without a clear separation of concerns.
  • Tasks cannot be separated. A graphics person cannot tweak the visual layout without at least some knowledge of the code, and vice versa.
  • Testing is messy at best. Difficult to verify the results of processing in a fashion which is independent of the visual layout
  • Editors generally aren’t big fans of mixing two separate languages within each other. Automatic indentation and other editor features might cease to work effectively.
  • The need to learn and use Yet Another Script Language of dubious merit.
  • Indentation of the resulting HTML easily becomes totally messed up (or alternatively the indentation of the template).
  • You cannot view a template file with a normal browser, without passing it through processing. Thus it does not act as an actual prototype for the resulting page.
  • Actual code reuse seems to be low.

Many of these points can be applied to any other template system similar to Template Toolkit. Additionally I find the incoherent syntax of the language to be annoying and at least the version I’ve been using does not seem to want to crash if accessing a variable or function which does not exist. Instead it tries to be clever and applies empty values. The final HTML is also left with big white spaces where template code was run (and yes, we tried the whitespace joining option, but that resulted in other problems).

Some of these issues can be resolved by careful TT coding and template modularisation. But then again, why bother? If you are going about separating things then why not do it with any mature script language with rich support for modularisation, objects, test facilities etc.

I spent a day coding a new template system in Tcl. It is based on the idea that templates merely refer to objects by class name. These then provide the actual values and content for filling in relevant parts. A separate Tcl script is evaluated to create those objects. The objects can simply be structures with the required fields, or they can contain methods which calculate the values. This is all invisible to the template. It doesn’t care how things are done.

The templates themselves contain dummy values which can be viewed with any web browser, thus allowing you to tweak the layout to your heart’s desire, without touching a line of code.

I dubbed the system ‘Illusion’ because it is clever at hiding things and the result happens as if by magic, without either party knowing about each other. It is simple and effective and although it is still lacking a few things which I have in mind, I believe it proves the concept and you can play around with it.

For now it’s available as a git repository. You can get it with the following:

git clone http://people.fishpool.fi/~setok/git/Illusion.git

Note that it requires at least Tcl 8.4 and XOTcl.


Leave a Reply