Last active
August 29, 2015 14:06
-
-
Save andsens/2e876a2def9c5cd8e3c0 to your computer and use it in GitHub Desktop.
Renders a jinja2 template and outputs the result
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
#!/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