【Python】【Bottle】 Templates

■Templates

テンプレートを使用するには「template」関数を使用する。
テンプレートの配置は「./views/」フォルダ、もしくは
「bottle.TEMPLATE_PATH」に登録されているパスを読み込んでくれる。
なので読み込んでほしいフォルダを指定する場合は
「bottle.TEMPLATE_PATH.insert(0,'./template/')」
とすることで「template」フォルダを読み込んでくれるようになる。

index.py
# coding:utf-8
from bottle import route, request, response, template, view

@route('/lucky')
def lucky(no='9'):
return template('tmplate_test.tpl', no=no)

@route('/unlucky')
def lucky(no='!9'):
return template('tmplate_test.tpl', no=no)

if __name__ == '__main__':
bottle.TEMPLATE_PATH.insert(0,'./template/')
run(host='127.0.0.1', port=8080, debug=True, reloader=True)
tmplate_test.tpl
%if no == '9':
<h1>LucyNo.{{no}}!</h1>
%else:
<h1>UnLucy!</h1>
%end
テンプレートを使用する方法は他にviewデコーダを使用して
指定する方法もあるようだが、上手くテンプレートを見つけてくれなかったようなので
時間を見計らって検証してみようと思う。