When to use the Template Engine in Python?

As a “newbie” in Python and mainly having scripting experience to automate tasks related to system administration, I don’t have much sense where to use certain tools.

But I am very interested in developing instincts about where to use specific tools / methods.

I've seen a lot of mentions about engine templates, including zedshaw talking about using Jinja2 in Lamson

So I thought I would ask the community to give me some tips on where / when I might know that I should consider using a template engine like Jinja2 , Genshi , Mako and co.

+4
source share
3 answers

According to @mikem, templates help generate any form of output you like in the right conditions. In fact, the first significant thing I've ever written in Python is the template system - YAPTU , for another utility for the Python pattern - and that was 8+ years ago, before other good systems like that existed .. shortly after I had the honor to increase it no less than Peter Norvig (see here ), and now it has nearly 2,000 hits on search engines; -).

Today engine templates are much better in many ways (although most of them are pretty specialized, especially for HTML output), but the main idea remains: why worry about the many print statements and hard-coded lines in Python code, when it’s even easier to output lines to editable template files ? If you ever want (for example) to be able to release in French, English or Italian (it was the original motivation of YAPTU during intensive hacking weekends when I first became acquainted with Python ...! -), being able to simply getting your templates from the correct directory (where the text is properly translated) will make everything a lot easier !!!

Essentially, I think that a templating system will most likely not be a good idea whenever you output text files. I used YAPTU or its adaptation to smoothly switch between JSON, XML and human-readable (HTML, actually) output, for example; a really good template system these days, in fact, should actually exceed the text-ish limit and output it to protobuf or another binary serialization format.

Which template system will best depend on your specific situation - in your shoes, I would study (and experiment) with some of them, anyway. Some of them are intended for cases when the user interface developers (who cannot program) must edit them, others only for programmers; many of them specialize in HTML, some specialize in XML, others more general; etc. etc. But SOME one of them (or your own one more!) - will surely be better than a bunch of print s! -)

+6
source

The template engine runs on templates to programmatically generate artifacts. The template defines 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 (i.e. the SPEC RPM file), Python code that can be executed later, etc.

+2
source

Simply put, templates make it easy to write a humanoid document by hand and add very simple markup to identify areas that should be replaced by variables, the area that should be repeated, etc.

Typically, the template language can only perform the basic “logic of templates”, which means enough logic to affect the layout of the document, but not enough to affect the data that was submitted to the template to fill the document.

0
source

Source: https://habr.com/ru/post/1286609/


All Articles