No new web templating languages; use an existing one

November 11, 2015

Suppose, hypothetically, that you are creating a web application. Let's even suppose that it's a very small and simple one, almost an embarrassingly small one. As part of this app, you need a very little bit of something like a templating system. Not much, just a bit more than printing formatted strings. Clearly you have such a trivial situation that you can just bang together a tiny and simple mini-templating language, right?

Let me save you some time and effort: no. Don't do it. The reality is that we've reached a point in time where writing your own (web) templating language or system is basically guaranteed to be a mistake. I know, you have a trivial application and you don't want to take an external dependency, you hardly need anything, all of the existing templating systems are wrong or too heavyweight, there's a whole list of excuses. Don't accept them. Suck it up, take an external dependency, and use an existing templating system even if it's vast overkill for your problem. Your future self will thank you in a few years.

(I could almost go further than this and maybe I should, but that's another entry.)

All of this especially applies if you have an application that's needs more than a trivial templating system; I picked an extreme case because it's where the temptation can be strongest. Writing your own non-tiny templating system today is an especially masochistic exercise because even a basic one is a bunch of work and raises a moderate ton of questions that you're ill-equipped to answer (or even recognize) unless this is not your first templating system.

In hindsight, writing my own 'simple' templating system was one of the mistakes I made when I wrote DWiki (the code that powers Wandering Thoughts). It's been a very educational mistake, but unless you really want to do things the hard way for the experience I can't recommend it.

(Note that rolling your own is not a great learning experience unless you live with the result for a number of years, so that you have plenty of time to run into the lurking problems. Almost anything can look good if you write it, use it briefly, and then abandon it. Many of my painful lessons took years to smack me in the face.)

PS: This assumes that you aren't working in a new language where no one's written a decent templating system. If you are, I think that you should at least steal one of the battle-tested designs from good templating systems in other languages.

(Also, yes, a very few people have very special needs and have to write their own systems. They know who they are.)

Comments on this page:

I went down that road and I’ve been hating the result for a decade. But I guess the point is that most likely you’ll wind up with something you hate either way, and it’s better to choose the more fully featured hatefulness that you don’t have to write yourself over the underpowered one you do.

(I have yet to see a templating engine I like. Genshi looks like a contender, but I’ve never used Python for much of anything so I never got to give it a try. These days I think I’d want something built on a different paradigm (completely static HTML templates with selector-centric external logic and no mini-language), though I’m not sure, since I’ve not seen that done really well anywhere yet.)

By Anon at 2015-11-12 01:02:59:

So, what are your thoughts on ?

By dozzie at 2015-11-12 06:17:48:

Jinja is quite sensible templating system, but it (deliberately) lacks some expressiveness. Also, it's very, very hard to manage whitespaces with Jinja, especially regarding the right amount of empty lines. I guess it tooks its trimming syntax after Template::Toolkit, but TT is much better.

By cks at 2015-11-13 14:19:20:

I don't have any particular opinions on specific template systems because I've only used one (Django's native system). My general view is that in most projects the exact choice of template system probably isn't going to make much of a difference. It's infrastructure, like a lot of other parts of the web app stack; once you get something that is good enough, further improvements are relatively incremental.

(There are exceptions, of course. You could be doing very sophisticated things, or modifying templates frequently, or running a high load site so that you care very much about efficiency, or something on this order. Then you probably care a lot about the exact template system because it makes a real difference in your life. For me, with our Django app? No.)

Written on 11 November 2015.
« Why I spent a lot of time agonizing over an error message recently
Don't have support registrations in the name of a specific sysadmin »

Page tools: View Source, View Normal, Add Comment.
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Wed Nov 11 01:13:33 2015
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.