Flask集成Fanstatic资源发布系统。
项目描述
Flask-Fanstatic
Flask集成Fanstatic资源发布系统。
概述
Fanstatic是一个灵活的系统,用于管理您的Web应用程序使用的静态资源(CSS和JavaScript)。此扩展提供了Fanstatic与Flask之间的简单集成。
将静态资源添加到您的应用程序变得像使用pip安装它们一样简单
pip install js.jquery
并在模板中需要它们
{{ g.fanstatic.needs('js.jquery:jquery') }}
用法
要开始使用Flask-Fanstatic,请导入并初始化扩展以用于您的Flask应用程序
from flask import Flask from flask_fanstatic import Fanstatic app = Flask(__name__) fanstatic = Fanstatic(app)
然后,在您的基模板中,添加top和bottom资源以在HTML中包含它们
<head> {{ g.fanstatic.top }} </head> <body> ...content... {{ g.fanstatic.bottom }} </body>
您可以使用needs()辅助函数声明模板所需的资源
{{- g.fanstatic.needs('js.jquery:jquery') -}} {% extends 'layout.html' %} ...
Fanstatic将使用上面的top和bottom辅助函数自动包含所需的CSS或JavaScript资源。
您也可以需要多个资源
{{ g.fanstatic.needs( 'js.jquery:jquery', 'js.handlebars:handlebars' ) }}
needs()方法接受任意数量的字符串,形式为<module>:<resource>。您也可以从您的代码中导入资源并要求它们,如下所示
from js.jquery import jquery @app.route('/') def index(): jquery.need() return render_template('index.html')
应用程序资源
Flask-Fanstatic还简化了将您应用程序的自己的静态文件添加为Fanstatic资源的过程。
您可以使用 resource() 辅助函数在应用程序的 'static' 文件夹中声明资源
fanstatic.resource('js/app.js', name='app_js', depends=[jquery])
要包含资源,只需在模板中使用其 name 来要求它
{{ g.fanstatic.needs('app_js') }}
您还可以声明命名的资源组
from js.jquery import jquery fanstatic.resource('css/app.js', name='app_js') # there are 3 ways to specify a group resource item: fanstatic.group('app_resources', [ # with an imported resource: jquery, # with the name of an internal resource: 'app_js', # with an inline resource: fanstatic.resource('css/app.css'), ])
组以相同的方式在模板中包含
{{ g.fanstatic.needs('app_resources') }}
蓝图资源
蓝图也可以像应用程序资源一样使用 Fanstatic。首先为您的蓝图初始化一个 Fanstatic() 对象,并声明其资源
bluep = Blueprint('bluep', __name__, static_folder='static') fanstatic = Fanstatic(bluep) fanstatic.resource('bluep.css', name='bluep_css')
在模板中,将当前蓝图中的资源引用为 .<name>
{{ g.fanstatic.needs('.bluep_css') }}
或者明确提供蓝图名称,以包含特定蓝图中的资源
{{ g.fanstatic.needs('bluep.bluep_css') }}
变更
0.2.0 (2014-01-04)
支持 Fanstatic 1.0
0.1.1 (2013-05-08)
修复了带有 URL 前缀的应用程序
0.1.0 (2012-11-19)
首次发布
项目详情
关闭
Flask-Fanstatic-0.2.0.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | e819a2013e4c3a398ba1dd76670d9e83d35b7f2aa67539c5928fc49f5c69b4f3 |
|
MD5 | 24682497166583abf1cc53b6ef13be24 |
|
BLAKE2b-256 | 17f224826869906c38d751de616c983ccfab96472d4f39d302c3dff7fc7133ed |