Last active
April 13, 2016 15:52
-
-
Save SpotlightKid/5446d37fb8705f6b2750 to your computer and use it in GitHub Desktop.
Jinja2 Quick Load Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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