Best Python templating library to facilitate code generation

Question:

Instead of me spending the next day (or year) reading about them all, are there any suggestions for templating engines that I should look into in more detail?

Asked By: taudep

||

Answers:

Best suggestion: try them all. It won’t take long.

My favourite: Jinja2 (by a mile)

It has decent syntax, can trace errors through it, and is sandboxable.

Answered By: Ali Afshar

The most important concern is whether you can live with the syntax the templates require. Second and third (depending on your application needs) would be speed and ease of distribution.

I looked at all of them, but the only syntax I could stand was Jinja. Jinja has the advantage of supporting a lot of Python constructs, so it’s very easy to add snippets of functionality to the templates as needed, without coding special tags. Most of what requires tags in other template systems is handled by macros in Jinja.

Of course, if you’re looking for something easy and quick, it’s hard to beat the Python templating API in the core language.

Answered By: Chris B.

I like Clearsilver because it works with several different languages and it strictly enforces the separation between data and presentation. I previously used Cheetah and while it’s pretty nice, I didn’t like working with what sometimes seemed like a limited form of Python.

Answered By: David Z

If you’re working with X[HT]ML, one of the tag-based templating systems that can leave you with well-formed templates is a good move. I use PXTL, FWIW. (It can produce other formats, but if your emphasis isn’t XML or HTML it’d not be a sensible choice.)

I have an intense dislike for templating systems that claim to “help you separate business logic and presentation” by limiting expressions to their own Little Language. They don’t seem to understand that there is such as thing as “presentation logic”, and it can get sometimes complicated enough to need a Real Language like Python to run it. Having you kick out your presentation logic into the app with the business logic is so not a win. Avoid!

(The limited expression separate mini-language approach made some sense in JSP’s ‘EL’, as Java is too annoyingly verbose to use in a templating library. But we’ve got Python! It’s perfect for writing expressions in templates as it is; chopping functionality out and making the user learn another new language gains you nothing.)

Answered By: bobince

Update: Kid appears to have been succeeded by Genshi.

I’ve used Kid, which is I think one of the older systems. I’ve found it to be extremely solid, stable and reliable. It’s tag-based, so it’s nice for working with XML/HTML. It’s kind of interesting in that template functions are done as HTML attributes, not special blocks, i.e. {% … %}. However, some aspects of that (especially the way it does ‘includes’) can get pretty irksome. It also doesn’t seem to be developed actively or at all anymore.

It’s worth taking a look at if you want something that has been around for a while and has become quite stable. If you want something more recent, I’ve heard good things about both Genshi and Jinja.

Answered By: DNS

If you want a very lightweight option, try templete. It’s only like 80 lines of code in single module. Have a look here and here (it was published in a blog). I think it is a clever and very focused solution, if the features are sufficient for you.

Answered By: nikow

If you’re doing code generation, you might find Cog useful – it’s specifically for code generation, rather than being a generally applicable templating language.

Answered By: babbageclunk