templateフィルタを追加する
今のDjangoと同じなんだと思うけど適当な情報が見つからなかったので載せておく。
テンプレートフィルタを記述した、template.pyなどの名前のモジュールを用意する。
datetime.strftimeで日時をフォーマットするフィルタを作ってみた。
from django.template import Library register = Library() import datetime def format_datetime(value, arg=None): if value: if arg: return datetime.datetime.strftime(value, arg) return value register.filter(format_datetime)
追加したテンプレートフィルタを使いたいviewのモジュール(main.pyとか)のmain()で「このテンプレートフィルタが入っているモジュールを使う」と登録する。
def main(): import template webapp.template.register_template_library('template') application = webapp.WSGIApplication([ ('/', IndexHandler), ], debug=True) util.run_wsgi_app(application)
- appengine_config.pyで登録すれば全体に反映されそうな気がしたけどwebapp.pyの設定が見えないスコープなので無理
- templateってモジュール名がwebapp.templateとぶつかるのでmainの中でインポートしている。別名つけてインポートしても良いかも。