LoginSignup
1
3

More than 3 years have passed since last update.

responder 2.0.0 以降で jinja2 の filter を追加する

Posted at

TL;DR

Responder が Ver 2.0.0 になって api から jinja_env が消えて custom filter が使えず困ったので、取り敢えず動作するように解消する話

cutom filter と html

custom_filters.py
def css_filter(path):
    return f"./static/css/{path}"
_layout.html
...
<link rel="stylesheet" href="{{ 'style.css' | css }}">
...

1.3.2 まで

1.3.2までは以下の書き方でfilterの追加が行えていた

app.py
import responder
import custom_filters

api = responder.API()


api.jinja_env.filters.update(
    css=custom_filters.css_filter
)

2.0.0 以降

2.0.0 以降は jinja_env がいないので、取り敢えず以下のように編集した

app.py
import responder
import custom_filters

api = responder.API()


api.templates._env.filters.update(
    css=custom_filters.css_filter
)

_ 付きなので responder 的には、あまり参照することを推奨していないかもしれない...

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3