ソースコード
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from jinja2 import Environment, FileSystemLoader
import yaml
def main():
# load template
env = Environment(loader=FileSystemLoader('./', encoding='utf8'))
tmpl = env.get_template('template.j2')
# load object
f = open('data.yml')
obj = yaml.load(f)
f.close()
# mix template and object
output = tmpl.render(obj).encode('utf-8')
print output
if __name__ == '__main__':
main()
---
key1 : val1
key2 : val2
items :
- alpha
- beta
- gamma
- delta
{# Comment #}
Hello Template
key1 = {{ key1 }}
key2 = {{ key2 }}
{% set cnt = 1 %}
{% for item in items %}
{{cnt}} = {{ item }}
{%- set cnt = cnt + 2 %}
{%- endfor %}