python-scripts/scripts/16_jinja_quick_load.py

25 lines
618 B
Python
Raw Normal View History

2014-06-13 17:52:30 +02:00
"""
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)