Insert static files literally into Jinja templates without parsing them

前端 未结 5 1744
逝去的感伤
逝去的感伤 2020-12-15 07:09

I\'m trying to insert file into a page using Jinja 2.6 using the include tag. This worked fine until I started using characters in the file that are reminiscent

5条回答
  •  渐次进展
    2020-12-15 07:34

    If you are using Flask it can be written like this:

    from jinja2 import Markup
    
    ...
    
    app.jinja_env.globals['include_raw'] = lambda filename : Markup(app.jinja_loader.get_source(app.jinja_env, filename)[0])
    

    And used like this:

    {{ include_raw('js-inline/modernizr.min.js') }}
    

    Path of the included file is relative to your template folder as for normal includes.

提交回复
热议问题