Skip to content

Instantly share code, notes, and snippets.

@develmaycare
Created March 12, 2017 23:02
Show Gist options
  • Save develmaycare/15b33411825a0c911cec2b012e221245 to your computer and use it in GitHub Desktop.
Save develmaycare/15b33411825a0c911cec2b012e221245 to your computer and use it in GitHub Desktop.
An example of how to use Jinja as a standalone parser. This is especially useful for command line packages.
# Perhaps there is a better way to do this, but this worked for parsing templates in pyprojectutils.
def parse_jinja_template(path, context):
# The path is the path to the template. One directory up becomes the search path.
search_path = os.path.dirname(path)
env = Environment(loader=FileSystemLoader(search_path))
# The template name is at the end of the path in this case.
template_name = os.path.basename(path)
template = env.get_template(template_name)
# Then it's just a normal render.
return template.render(**context)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment