Skip to content

Instantly share code, notes, and snippets.

@andsens
Last active August 29, 2015 14:06
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 andsens/2e876a2def9c5cd8e3c0 to your computer and use it in GitHub Desktop.
Save andsens/2e876a2def9c5cd8e3c0 to your computer and use it in GitHub Desktop.
Renders a jinja2 template and outputs the result
#!/usr/bin/env python
# Dependencies: jinja2, pyyaml, docopt
import docopt
usage = """jjrender
Renders a jinja2 template and outputs the result
Pipe the variables in yml format into stdin
Alternatively you can simply paste the tpl after starting jjrender, terminate input with Ctrl+D
Usage: jjrender TEMPLATE
Options:
-h, --help show this help
"""
opts = docopt.docopt(usage)
from jinja2 import Template
with open(opts['TEMPLATE'], 'r') as f:
tpl = Template(f.read())
import sys
stdin = ''.join(sys.stdin.readlines())
import yaml
data = yaml.safe_load(stdin)
if data is None:
data = {}
print tpl.render(**data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment