Created
December 28, 2014 12:21
-
-
Save bcdejp/00619c891bb83634e2a6 to your computer and use it in GitHub Desktop.
jinja2のサンプル
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 | |
# -*- coding: utf-8 -*- | |
from jinja2 import Environment, FileSystemLoader | |
#テンプレートファイルを指定 | |
env = Environment(loader=FileSystemLoader('./', encoding='utf8')) | |
tpl = env.get_template('template.html') | |
#テンプレートへ挿入するデータの作成 | |
title = u"これはタイトルです" | |
sample_list = [] | |
sample_list.append({'title':u"コンテンツtitle1", 'body':u"コンテンツbody1"}) | |
sample_list.append({'title':u"コンテンツtitle2", 'body':u"コンテンツbody2"}) | |
sample_list.append({'title':u"コンテンツtitle3", 'body':u"コンテンツbody3"}) | |
#テンプレートへの挿入 | |
html = tpl.render({'title':title, 'sample_list':sample_list}) | |
#ファイルへの書き込み | |
tmpfile = open("generate.html", 'w') #書き込みモードで開く | |
tmpfile.write(html.encode('utf-8')) | |
tmpfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment