views:

218

answers:

3

As a "newbie" to Python, and mainly having a background of writing scripts for automating system administration related tasks, I don't have a lot of sense for where to use certain tools.

But I am very interested in developing instincts on where to use specific tools/techniques.

I've seen a lot mentioned about templating engines, included zedshaw talking about using Jinja2 in Lamson

So I thought I would ask the community to provide me with some advice as to where/when I might know I should consider using a templating engine, such as Jinja2, Genshi, Mako, and co.

+2  A: 

A templating engine works on templates to programmatically generate artifacts. A template specifies the skeleton of the artifact and the program fills in the blanks. This can be used to generate almost anything, be it HTML, some kind of script (ie: an RPM SPEC file), Python code which can be executed later, etc.

Mike Mazur
+4  A: 

As @mikem says, templates help generating whatever form of output you like, in the right conditions. Essentially the first meaningful thing I ever wrote in Python was a templating system -- YAPTU, for Yet Another Python Templating Utility -- and that was 8+ years ago, before other good such systems existed... soon after I had the honor of having it enhanced by none less than Peter Norvig (see here), and now it sports almost 2,000 hits on search engines;-).

Today's templating engines are much better in many respects (though most are pretty specialized, especially to HTML output), but the fundamental idea remains -- why bother with a lot of print statements and hard-coded strings in Python code, when it's even easier to hve the strings out in editable template files? If you ever want (e.g.) to have the ability to output in French, English, or Italian (that was YAPTU's original motivation during an intense weekend of hacking as I was getting acquainted with Python for the first time...!-), being able to just get your templates from the right directory (where the text is appropriately translated) will make everything SO much easier!!!

Essentially, I think a templating system is more likely than not to be a good idea, whenever you're outputting text-ish stuff. I've used YAPTU or adaptations thereof to smoothly switch between JSON, XML, and human-readable (HTML, actually) output, for example; a really good templating system, in this day and age, should in fact be able to transcend the "text-ish" limit and output in protobuf or other binary serialization format.

Which templating system is best entirely depend on your specific situation -- in your shoes, I'd study (and experiment with) a few of them, anyway. Some are designed for cases in which UI designers (who can't program) should be editing them, others for programmer use only; many are specialized to HTML, some to XML, others more general; etc, etc. But SOME one of them (or your own Yet Another one!-) is sure to be better than a bunch of prints!-)

Alex Martelli
Excellent and helpful answer. I think I'll have a better sense of when I may "bump" into a situation where I should use a templating engine.
solarce
I've come a long way since posting this question, but potentially relevant to future searches who stumble upon it, is that I have been using a newer templating tool, http://github.com/defunkt/pystache for a recent project and find it to be a very nice lightweight templating tool.
solarce
A: 

Simply put, templating lets you easily write a human readable document by hand and add very simple markup to identify areas that should be replaced by variables, area that should repeat, etc.

Typically a templating language can do only basic "template logic", meaning just enough logic to affect the layout of the document, but not enough to affect the data that's fed to the template to populate the document.

Soviut