-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassets.py
85 lines (67 loc) · 2.03 KB
/
assets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# coding=utf-8
from flask_assets import Bundle, Environment
from webassets.filter import register_filter
from webassets.filter.jst import JST
import simplejson as json
# UnderscoreTemplateFilter
class UST(JST):
"""
继承jst filter,用``_.template``代替jst自带的template函数。window.JST是存放模板函数的命名空间。
生成的js文件类似下面:
(function(){
window.JST = window.JST || {};
window.JST['group'] = _.template("
<img src=\"/static/img/menu-cai-icon.png\"><span><%- group.title %></span>
");
})();
通过JST.group()来输出模板。
"""
name = 'ust'
def process_templates(self, out, hunks, **kwargs):
namespace = 'window.JST'
out.write("(function(){\n")
out.write("%s = %s || {};\n" % (namespace, namespace))
for name, hunk in self.iter_templates_with_base(hunks):
# Make it a valid Javascript string.
contents = json.dumps(hunk.data())
out.write("%s['%s'] = " % (namespace, self._get_jst_name(name)))
out.write("%s(%s);\n" % ('_.template', contents))
out.write("})();")
register_filter(UST)
# 设置js和css文件打包
assets = Environment()
templates = Bundle(
'jst/group.jst',
'jst/group_summary.jst',
'jst/purchase.jst',
'jst/product.jst',
filters='ust',
output='gen/templates.js'
)
bundles = {
'layout': Bundle(
'css/reset.css',
'css/m.css',
output='gen/layout.css',
filters='cssmin'
),
'leader_js': Bundle(
templates,
'js/models.js',
'js/group_list_view.js',
'js/product_list_view.js',
'js/group_summary_view.js',
output='gen/leader.js',
filters='jsmin'
),
'member_js': Bundle(
templates,
'js/models.js',
'js/group_list_view.js',
'js/purchase_list_view.js',
'js/group_summary_view.js',
output='gen/member.js',
filters='jsmin'
)
}
assets.register(bundles)