Code
import jinja2
= jinja2.Environment()
environment = environment.from_string("Hello, {{name}}!")
template ="World") template.render(name
'Hello, World!'
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.
Jinja2
'Hello, World!'
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 {expr} %}
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 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.