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.
Template engines are used when you want to rapidly build web applications that are split into different components. Templates also enable fast rendering of the server-side data that needs to be passed to the application. For example, you might want to have components such as body, navigation, footer, dashboard, etc.
A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
Templating in Python. Templating, and in particular web templating is a way to represent data in different forms. These forms often (but not always) intended to be readable, even attractive, to a human audience. Frequently, templating solutions involve a document (the template) and data.
Jinja2 is a modern day templating language for Python developers. It was made after Django's template. It is used to create HTML, XML or other markup formats that are returned to the user via an HTTP request.
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 print
s!-)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With