Skip to content

Instantly share code, notes, and snippets.

@SpotlightKid
Last active April 13, 2016 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SpotlightKid/5446d37fb8705f6b2750 to your computer and use it in GitHub Desktop.
Save SpotlightKid/5446d37fb8705f6b2750 to your computer and use it in GitHub Desktop.
Jinja2 Quick Load Function
# Source: http://pydanny.com/jinja2-quick-load-function.html
from jinja2 import FileSystemLoader, Environment
def render_from_template(directory, template_name, **kwargs):
"""Render named Jinja2 template in given direcctory.
Sample usage::
>>> from mymodule import render_from_template
>>> data = {
... "date": "June 12, 2014",
... "items": ["oranges", "bananas", "steak", "milk"]
... }
>>> render_from_template(".", "shopping_list.html", **data)
"""
loader = FileSystemLoader(directory)
env = Environment(loader=loader)
template = env.get_template(template_name)
return template.render(**kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment