Skip to content

Instantly share code, notes, and snippets.

@half-duplex
Created May 14, 2015 04:08
Show Gist options
  • Select an option

  • Save half-duplex/09d7e005ddbc225852fc to your computer and use it in GitHub Desktop.

Select an option

Save half-duplex/09d7e005ddbc225852fc to your computer and use it in GitHub Desktop.
jinja2 erb style __main__
# -*- coding: utf-8 -*-
"""
jinja2.__main__
~~~~~~~~~~~~~~~
Allows for simple rendering in the style of eRuby, accepting variables from
the environment.
:copyright: (c) 2015 by the Jinja Team.
:license: BSD, see LICENSE for more details.
"""
import os
import sys
from jinja2.environment import Environment
from jinja2.loaders import FileSystemLoader
if len(sys.argv) < 2:
raise Exception("Run like: my_variable=Arnold python -m jinja2 my_template.jinja")
template_file = sys.argv[1]
loader = FileSystemLoader(searchpath=os.getcwd())
template_env = Environment(loader=loader)
template = template_env.get_template(template_file)
output = template.render(os.environ)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment