Python Library: jinja2

A very fast and expressive template engine.

04 Oct 2022

Library resources
PyPI https://pypi.org/project/Jinja2/
Github https://github.com/pallets/jinja/
Documentation https://jinja.palletsprojects.com/en/latest/
Homepage https://palletsprojects.com/p/jinja/

Getting Started

pip3 install Jinja2

Usage

{% extends "layout.html" %}
{% block body %}
  <ul>
  {% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
  {% endfor %}
  </ul>
{% endblock %}

To load a template from this environment, call the get_template() method, which returns the loaded Template.

template = env.get_template("mytemplate.html")

To render it with some variables, call the render() method.

template.render(the="variables", go="here")

Learnings

If you comment out an insertion block (ie {% include 'xxxx.html' %}), it will still be inserted in the output!
Lost 45mns figuring that one out!

Resources

links

social