2010年06月19日
Google App Engine Djangoのテンプレートエンジン
Google App EngineでDjangoのテンプレートエンジンの使い方です。
1.テンプレートファイルの作成
views/index.html
<html>
<body>
<h1>{{ title }}</h1>
<p>{{ message }}</p>
</body>
</html>
2.モジュールの読み込み
「os」、「template」モジュールを読み込みます。main.py
import os from google.appengine.ext.webapp import template
3.テンプレートファイルのレンダリング、表示
main.py
class MainHandler(webapp.RequestHandler):
def get(self):
template_values = {
'title': 'Hello world!',
'message': 'hogehogemogemoge!'
}
path = os.path.join(os.path.dirname(__file__), 'views', 'index.html')
self.response.out.write(template.render(path, template_values))
os.path.join()でテンプレートファイルまでのパス(views/index.html)を組み立てます。
template.render()でレンダリング結果を取得して、
response.out.write()で表示します。
template.render()の第2引数にテンプレートに渡す変数をセットしています。
以上です。
簡単ですね。






