π₯· Jinjaο
Jinja is a template engine, which allows you to generate text from a template.
Installationο
Jinja2 is the version compatible with python3. You can install using pip:
pip install Jinja2
Basic Usageο
With Jinja you provide a template file with variables that should be filled in. You load this file and then βrenderβ these variables.
from jinja2 import Environment, FileSystemLoader
name = "jinja"
environment = Environment(loader=FileSystemLoader("templates/"))
template = environment.get_template("message.txt")
# returns rendered template as a string
content = template.render(name=name)
print(content)
Hello, my name is {{ name }}.
Flow Controlο
if statementsο
{% if score < 80 %}
You FAILED
{% else %}
You PASSED
{% endif %}
for loopsο
{% for student in students %}
Hello, my name is {{ student }}
{% endfor %}
Nesting templatesο
As you get more templates, you might want to keep common code in the same place. You can use Jinjaβs template inheritance.
Hello my name is {{ name }}.
{% block age %} By default my age is {{ age }} {% endblock age %}
{% extends "base.template" %}
{% block age %} My new age is {{ age }} {% endblock age %}
If the block age is not given in the overriding template, then the content from the default template will be used.
Including Templatesο
You are also able to include a whole other template in another template.
Note
Template files which are meant to be included should be prefixed with an underscore in their name.
{% include "_other.template" %}
Some more words.
Whitespaceο
You need take care about where Jinja will add in whitespace.
Hello
{# Just a comment #}
You
Hello
You
There are a few ways you can avoid this.
Enable
trim_blocksandlstrip_blocksrendering optionsUse a
-at the start or end of a block. e.g.{% if var == "1" -%}
Built-in Filtersο
You can see a list of built-in filters here: https://jinja.palletsprojects.com/en/3.1.x/templates/#builtin-filters
You can apply a filter by using the pipe symbol |.
First name: {{ first_name | capitalize }}
{{ scraped_acl | first | trim }}
Jinja allows you to write your own filters too. These are basically just python functions. See https://ttl255.com/jinja2-tutorial-part-4-template-filters/#:~:text=on%20our%20list.-,Writing%20Your%20Own%20Filters,-As%20I%20already for more info.
Macrosο
Macros are nice for creating re-usable components.
{% macro banner() -%}
===========================================
| This device is property of BigCorpCo |
| Unauthorized access is unauthorized |
| Unless you're authorized to access it |
| In which case play nice and stay safe |
===========================================
{% endmacro -%}
{{ banner() }}
===========================================
| This device is property of BigCorpCo |
| Unauthorized access is unauthorized |
| Unless you're authorized to access it |
| In which case play nice and stay safe |
===========================================
You are also able to pass arguments to macro calls.
{% macro def_if_desc(if_role) -%}
Unused port, dedicated to {{ if_role }} devices
{%- endmacro -%}
Macros are similar to python function in that they also can have args and kwargs.
Macros in separate fileο
You can store macros in a separate file and import them into your file as needed.
For example, to import: {% import 'macros.file' as macros -%}.
You can access a macro by using macro.<macro_name>.
Commentsο