jinja2 template from file code example

Example 1: jinja render template to file

from jinja2 import Environment, FileSystemLoader

env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('test.html')

output_from_parsed_template = template.render(foo='Hello World!')
print(output_from_parsed_template)

# to save the results
with open("my_new_file.html", "w") as fh:
    fh.write(output_from_parsed_template)

Example 2: python jinja2 from string

from jinja2 import Environment, BaseLoader

rtemplate = Environment(loader=BaseLoader()).from_string(myString)
data = rtemplate.render(**data)

Example 3: jinja2 include template

{% include "to_be_included.html" with context %}