Welcome to my field notes!

Field notes are notes I leave myself as I go through my day to day work. The hope is that other people will also find these notes useful. Note that these notes are unfiltered and unverified.

Jinja Templating

Author

TJ Palanca

Published

September 3, 2022

Basics

  • Install from pip: Jinja2
  • You can render from a literal template string
Code
import jinja2

environment = jinja2.Environment()
template = environment.from_string("Hello, {{name}}!")
template.render(name="World")
'Hello, World!'
  • Can also render from a template file, the template variables can be passed through keyword arguments, but also a dictionary that will be unpacked (if no keyword).
Code
from jinja2 import Environment, FileSystemLoader

max_score = 100
test_name = "Python Challenge"
students = [
    {"name": "Sandrine", "score": 100},
    {"name": "Gergeley", "score": 75},
    {"name": "Friedaly", "score": 92}
]

environment = Environment(loader=FileSystemLoader("."))
template = environment.get_template("message.txt")

def render_emails(students, template, max_score, test_name):
    with open(template.filename) as file:
        print(file.read())
    for student in students:
        print(template.render(
            student, 
            max_score=max_score, 
            test_name=test_name
        ))

render_emails(students, template, max_score, test_name)
{# templates/message.txt #}

Hello {{ name }}!

I'm happy to inform you that you did very well on today's {{ test_name }}.
You reached {{ score }} out of {{ max_score }} points.

See you tomorrow!
Anke



Hello Sandrine!

I'm happy to inform you that you did very well on today's Python Challenge.
You reached 100 out of 100 points.

See you tomorrow!
Anke


Hello Gergeley!

I'm happy to inform you that you did very well on today's Python Challenge.
You reached 75 out of 100 points.

See you tomorrow!
Anke


Hello Friedaly!

I'm happy to inform you that you did very well on today's Python Challenge.
You reached 92 out of 100 points.

See you tomorrow!
Anke
  • If statements and for loops can be embedded in Jinja using {% if {expr} %}
Code
template = environment.get_template("message_if.txt")
render_emails(students, template, max_score, test_name)
Hello {{ name }}!

{% if score > 80 %}
I'm happy to inform you that you did very well on today's {{ test_name }}.
{% else %}
I'm sorry to inform you that you did not do so well on today's {{ test_name }}.
{% endif %}

You reached {{ score }} out of {{ max_score }} points.

See you tomorrow!
Anke

Hello Sandrine!


I'm happy to inform you that you did very well on today's Python Challenge.


You reached 100 out of 100 points.

See you tomorrow!
Anke
Hello Gergeley!


I'm sorry to inform you that you did not do so well on today's Python Challenge.


You reached 75 out of 100 points.

See you tomorrow!
Anke
Hello Friedaly!


I'm happy to inform you that you did very well on today's Python Challenge.


You reached 92 out of 100 points.

See you tomorrow!
Anke
  • For loops can be used to render repetitive content.
Code
template = environment.get_template("message_for.txt")
with open(template.filename) as file:
    print(file.read())
print(template.render(students=students, max_score=max_score, test_name=test_name))
{% for student in students %}

Hello {{ student.name }}!

{% if student.score > 80 %}
I'm happy to inform you that you did very well on today's {{ test_name }}.
{% else %}
I'm sorry to inform you that you did not do so well on today's {{ test_name }}.
{% endif %}

You reached {{ student.score }} out of {{ max_score }} points.

See you tomorrow!
Anke

{% endfor %}



Hello Sandrine!


I'm happy to inform you that you did very well on today's Python Challenge.


You reached 100 out of 100 points.

See you tomorrow!
Anke



Hello Gergeley!


I'm sorry to inform you that you did not do so well on today's Python Challenge.


You reached 75 out of 100 points.

See you tomorrow!
Anke



Hello Friedaly!


I'm happy to inform you that you did very well on today's Python Challenge.


You reached 92 out of 100 points.

See you tomorrow!
Anke

Other features can be explored but these are some basics.