views:

638

answers:

8

It's been awhile since I've done something like this and it seems that everyone and their grandmother has created a templating library for python, from Mako, Cheetah, Genshi, etc...

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

Thanks

+8  A: 

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.

Ali A
It seems that I've gotten a couple answers for Jinja2 -- where heretofore I never heard about. Now with apprpriate Google searches, and even here at StackOverflow, I'm off and running. Thanks.
taudep
+1  A: 

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.

Chris B.
+1  A: 

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.

David Zaslavsky
+1  A: 

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.)

bobince
+1  A: 

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.

http://www.kid-templating.org/

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.

DNS
Genshi is Kid's official successor.
joeforker
A: 

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.

nikow
+3  A: 

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.

wilberforce
Hello! I like the way cog works for code generation, but I cannot get it to work, 'cause it needs a module called 'path' which I am not able to find, could you help me out with this?
Shrikant Sharat
Hmm - it's in the cheeseshop, but the site it points to is dead. The text of (a version of) the module is in the Python wiki: http://wiki.python.org/moin/PathModule. Not sure whether that one will work with Cog. It'd be worth asking Ned Batchelder (Cog's author) if he could start bundling path.py, if it's been abandoned.
wilberforce
+1  A: 

I think Django templating is the best i have ever came across.

aatifh
You should try Jinja. It's based on Django, but it's been redone from scratch to work better.
Chris B.