Jinja2 Environments

So to be able to call different files/template that are not in the current directory,

 

from __future__ import unicode_literals, print_function
from jinja2 import FileSystemLoader, StrictUndefined
from jinja2.environment import Environment

#env = Environment()  <--- load the default environment 
#env = Environment(undefined=StrictUndefined) <--- specify the script to fail if a variable isnt define
#env.loader = FileSystemLoader(".") <--- look in the current directory
#env.loader = FileSystemLoader('/templates/")

template_vars = {}

template_file = "template1.j2"
template = env.get_template(template_file)
output = template.render(**template_vars)
print(output)

Leave a Comment