テンプレートを処理して表示する

説明

テンプレートを処理して表示するにはtemplate.render()を使います。最初のパラメータにはテンプレートファイルのパス、2番目のパラメータには{}を使ってキーの名前と置換する値を指定します。複数ある場合には,(カンマ)で区切ってキーと値を列記します。

サンプルコード(抜粋)

text = 'Hello !!'
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(
template.render(path, { 'msg' : text} )
)

●テンプレートとなるHTMLファイル (index.html)
<body>
<h1>template Sample</h1>
テンプレートのサンプルです。<br>
{{ msg }}
</body>
[サンプルをダウンロード]
[戻る]
写真素材 PIXTA