Using sass with Flask and jinja2

前端 未结 4 1625
我在风中等你
我在风中等你 2021-01-30 08:51

I would like to include a sass compiler in my Flask application. Is there a generally accepted way of doing this?

4条回答
  •  梦毁少年i
    2021-01-30 09:12

    Flask-Assets extension (which uses webassets library) can be used for that. Here's how to configure it to use pyScss compiler (implemented in Python) for SCSS:

    from flask import Flask, render_template
    from flask.ext.assets import Environment, Bundle
    
    app = Flask(__name__)
    
    assets = Environment(app)
    assets.url = app.static_url_path
    scss = Bundle('foo.scss', 'bar.scss', filters='pyscss', output='all.css')
    assets.register('scss_all', scss)
    

    And in the template include this:

    {% assets "scss_all" %}
    
    {% endassets %}
    

    SCSS files will be compiled in debug mode as well.

    pyScss only supports SCSS syntax, but there are other filters (sass, scss and compass) which use the original Ruby implementation.

提交回复
热议问题