Disable Jinja template caching for certain filter

前端 未结 4 1852
长情又很酷
长情又很酷 2020-12-19 14:10

We have a custom Jinja filter that we use for creating cache busting URL for our Javascript and CSS resources. We now noticed that in our production environment the final, c

4条回答
  •  孤城傲影
    2020-12-19 14:22

    After lots of Googling, I finally found the real solution to this. Jinja has a special helper called contextfilter that you can use to decorate your function with to make your filter context-aware (and context-dependent). The Jinja bytecode cache will not cache this computed value, even when a constant is passed as input.

    In your filter in Python:

    from jinja2 import contextfilter
    
    @contextfilter
    def asset_url(context, url):
        return some_url_thing(url)
    

    In your template:

    
    

提交回复
热议问题