Utilizing some advanced Jinja2 features
Chapter 1 introduced the Jinja2 engine and templating, and some of these Jinja constructs were applied to render HTML contents:
{{ variable }}
: The placeholder expression that renders a single-valued object from view functions.{% statement %
}: The expression that implementsif
-else
-conditions,for
-loops,block
-expressions for calling layout fragments,with
-blocks for managing context, and macro calls.
But some Jinja2 features, such as applying the with
-statement, macros, filters, and comments, can help generate better views for our routes.
Applying with-blocks and macros
In the Applying flash messages section, templates used the {% with %}
statement to extract the flash messages from the view functions and {% macro %}
in optimizing our Jinja2 transactions. The {% with %}
statement sets a context to limit the access or scope of some variables within the with
-block. Access outside the block produces a Jinja2 error.