【Python】【Bottle】 Templates
■Templates
テンプレートを使用するには「template」関数を使用する。
テンプレートの配置は「./views/」フォルダ、もしくは
「bottle.TEMPLATE_PATH」に登録されているパスを読み込んでくれる。
なので読み込んでほしいフォルダを指定する場合は
「bottle.TEMPLATE_PATH.insert(0,'./template/')」
とすることで「template」フォルダを読み込んでくれるようになる。
index.py
指定する方法もあるようだが、上手くテンプレートを見つけてくれなかったようなので
時間を見計らって検証してみようと思う。
テンプレートを使用するには「template」関数を使用する。
テンプレートの配置は「./views/」フォルダ、もしくは
「bottle.TEMPLATE_PATH」に登録されているパスを読み込んでくれる。
なので読み込んでほしいフォルダを指定する場合は
「bottle.TEMPLATE_PATH.insert(0,'./template/')」
とすることで「template」フォルダを読み込んでくれるようになる。
index.py
# coding:utf-8tmplate_test.tpl
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)
%if no == '9':テンプレートを使用する方法は他にviewデコーダを使用して
<h1>LucyNo.{{no}}!</h1>
%else:
<h1>UnLucy!</h1>
%end
指定する方法もあるようだが、上手くテンプレートを見つけてくれなかったようなので
時間を見計らって検証してみようと思う。