What is Jinja2 and how is it coupled with Flask?
Jinja2 is a library found at http://jinja.pocoo.org/; you can use it to produce formatted text with bundled logic. Unlike the Python format function, which only allows you to replace markup with variable content, you can have a control structure, such as a for
loop, inside a template string and use Jinja2 to parse it. Let's consider this example:
from jinja2 import Template x = """ <p>Uncle Scrooge nephews</p> <ul> {% for i in my_list %} <li>{{ i }}</li> {% endfor %} </ul> """ template = Template(x) # output is an unicode string print template.render(my_list=['Huey', 'Dewey', 'Louie'])
In the preceding code, we have a very simple example where we create a template string with a for
loop control structure ("for tag", for short) that iterates over a list variable called my_list
and prints the element inside a "li HTML tag"...