python-scripts/scripts/16_jinja_quick_load.py
2016-12-17 09:22:43 -07:00

25 lines
618 B
Python
Executable File

"""
Render a quick Jinja2 template.
Thanks Danny - http://pydanny.com/jinja2-quick-load-function.html
Example:
>>> from jinja_quick_load import render_from_template
>>> data = {
... "date": "June 12, 2014",
... "items": ["oranges", "bananas", "steak", "milk"]
... }
>>> render_from_template(".", "shopping_list.html", **data)
"""
from jinja2 import FileSystemLoader, Environment
def render_from_template(directory, template_name, **kwargs):
loader = FileSystemLoader(directory)
env = Environment(loader=loader)
template = env.get_template(template_name)
return template.render(**kwargs)