Created
March 14, 2016 16:39
-
-
Save timsavage/6554b93c2129216e6c66 to your computer and use it in GitHub Desktop.
Code Generation with Jinja
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
from jinja2 import Template, Environment | |
# The environment is used to configure the template engine | |
# See: http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment | |
env = Environment(autoescape=False, optimized=False) | |
# Open your template and compile into a template object. | |
with open("template.txt", "r") as f: | |
template = env.from_string(f.read()) | |
# Render the template | |
with open("/path/to/output-file.txt", "w") as f: | |
# Stream renders the template to a stream for writing to a file. | |
# Pass your variables as kwargs | |
template.stream( | |
Host="localhost", | |
IP="127.0.0.1", | |
BT=[12, 12, 32] | |
).dump(f) | |
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
Host: {{ Host }} | |
IP: {{ IP }} | |
{% for entry in BT %} | |
BT_{{ loop.index }}: {{ entry }} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment