mm_tpl 2.1.4 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cache/test.cache.html +8 -18
- package/hook.js +124 -0
- package/index.js +60 -10
- package/package.json +1 -1
- package/template/game/bar.html +7 -0
- package/template/game/test.html +11 -0
- package/test.js +18 -0
- /package/template/{test.html → defualt/test.html} +0 -0
package/cache/test.cache.html
CHANGED
|
@@ -5,23 +5,13 @@
|
|
|
5
5
|
<title>超级美眉Art模板引擎</title>
|
|
6
6
|
</head>
|
|
7
7
|
<body>
|
|
8
|
-
<
|
|
9
|
-
<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
</ul>
|
|
17
|
-
<ul>
|
|
18
|
-
<li>123</li>
|
|
19
|
-
<li>234</li>
|
|
20
|
-
</ul>
|
|
21
|
-
<script type="text/javascript">
|
|
22
|
-
var a_0 =123;
|
|
23
|
-
var a_1 =234;
|
|
24
|
-
</script>系统名称计算
|
|
25
|
-
123123'priceNIHAO'#计算
|
|
8
|
+
<div>游戏主题</div>
|
|
9
|
+
<div><div class="bar_user"><div>您好吗?</div><div>你好!</div></div>
|
|
10
|
+
<div class="bar">导航栏</div>
|
|
11
|
+
<style>
|
|
12
|
+
.bar {
|
|
13
|
+
font-weight: 800;
|
|
14
|
+
}
|
|
15
|
+
</style></div>
|
|
26
16
|
</body>
|
|
27
17
|
</html>
|
package/hook.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
class Hook {
|
|
2
|
+
constructor() {
|
|
3
|
+
this.dict_action = {};
|
|
4
|
+
this.dict_filter = {};
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 排序
|
|
10
|
+
* @param {Array} list 钩子列表
|
|
11
|
+
*/
|
|
12
|
+
Hook.prototype.sort = function(list) {
|
|
13
|
+
return list.sort((o1, o2) => {
|
|
14
|
+
return o2.sort - o1.sort
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 添加动作钩子
|
|
20
|
+
* @param {String} name 钩子名称
|
|
21
|
+
* @param {Function} func 钩子函数
|
|
22
|
+
* @param {Number} sort 执行顺序
|
|
23
|
+
* @param {String} alias 函数别名,用于删除
|
|
24
|
+
*/
|
|
25
|
+
Hook.prototype.addAction = function(name, func, sort, alias) {
|
|
26
|
+
var obj = {
|
|
27
|
+
func,
|
|
28
|
+
sort,
|
|
29
|
+
alias
|
|
30
|
+
}
|
|
31
|
+
var arr = this.dict_action[name];
|
|
32
|
+
if (!arr) {
|
|
33
|
+
arr = [];
|
|
34
|
+
}
|
|
35
|
+
arr.push(obj);
|
|
36
|
+
this.dict_action[name] = this.sort(arr);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* 添加过滤钩子
|
|
41
|
+
* @param {String} name 钩子名称
|
|
42
|
+
* @param {Function} func 钩子函数
|
|
43
|
+
* @param {Number} sort 执行顺序
|
|
44
|
+
* @param {String} alias 函数别名,用于删除
|
|
45
|
+
*/
|
|
46
|
+
Hook.prototype.addFilter = function(name, func, sort, alias) {
|
|
47
|
+
var obj = {
|
|
48
|
+
func,
|
|
49
|
+
sort,
|
|
50
|
+
alias
|
|
51
|
+
}
|
|
52
|
+
var arr = this.dict_filter[name];
|
|
53
|
+
if (!arr) {
|
|
54
|
+
arr = [];
|
|
55
|
+
}
|
|
56
|
+
arr.push(obj);
|
|
57
|
+
this.dict_filter[name] = this.sort(arr);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 删除动作钩子
|
|
62
|
+
* @param {String} name 钩子名称
|
|
63
|
+
* @param {String} alias 函数别名
|
|
64
|
+
*/
|
|
65
|
+
Hook.prototype.delAction = function(name, alias) {
|
|
66
|
+
var arr = this.dict_action[name];
|
|
67
|
+
if (arr) {
|
|
68
|
+
arr.del({
|
|
69
|
+
alias
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 删除过滤钩子
|
|
76
|
+
* @param {String} name 钩子名称
|
|
77
|
+
* @param {String} alias 函数别名
|
|
78
|
+
*/
|
|
79
|
+
Hook.prototype.delFilter = function(name, alias = "") {
|
|
80
|
+
var arr = this.dict_filter[name];
|
|
81
|
+
if (arr) {
|
|
82
|
+
arr.del({
|
|
83
|
+
alias
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 运行动作钩子
|
|
90
|
+
* @param {String} name 钩子名称
|
|
91
|
+
* @param {Object} ret 结果
|
|
92
|
+
* @param {Object} args 附加参数
|
|
93
|
+
*/
|
|
94
|
+
Hook.prototype.runAction = function(name, ret, ...args) {
|
|
95
|
+
var dict = this.dict_action;
|
|
96
|
+
var list = dict[name];
|
|
97
|
+
if (list) {
|
|
98
|
+
for (var i = 0; i < list.length; i++) {
|
|
99
|
+
var o = list[i];
|
|
100
|
+
ret += o.func(ret, ...args);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return ret;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 运行过滤钩子
|
|
108
|
+
* @param {String} name 钩子名称
|
|
109
|
+
* @param {Object} ret 结果
|
|
110
|
+
* @param {Object} args 附加参数
|
|
111
|
+
*/
|
|
112
|
+
Hook.prototype.runFilter = function(name, ret, ...args) {
|
|
113
|
+
var dict = this.dict_filter;
|
|
114
|
+
var list = dict[name];
|
|
115
|
+
if (list) {
|
|
116
|
+
for (var i = 0; i < list.length; i++) {
|
|
117
|
+
var o = list[i];
|
|
118
|
+
ret = o.func(ret, ...args);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return ret;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
module.exports = Hook;
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
require('mm_expand');
|
|
2
2
|
const template = require('art-template');
|
|
3
|
+
const Hook = require('./hook.js');
|
|
3
4
|
const {
|
|
4
5
|
nativeRule,
|
|
5
6
|
artRule,
|
|
@@ -13,6 +14,10 @@ if (!$.globalBag) {
|
|
|
13
14
|
$.globalBag = {};
|
|
14
15
|
}
|
|
15
16
|
|
|
17
|
+
if (!$.hook) {
|
|
18
|
+
$.hook = new Hook();
|
|
19
|
+
}
|
|
20
|
+
|
|
16
21
|
/**
|
|
17
22
|
* 模板类
|
|
18
23
|
* @class
|
|
@@ -30,6 +35,8 @@ class Tpl {
|
|
|
30
35
|
*/
|
|
31
36
|
this.config = {
|
|
32
37
|
root: $.runPath + '',
|
|
38
|
+
defualt_dir: "./template/",
|
|
39
|
+
defualt_theme: 'defualt',
|
|
33
40
|
extname: ".html",
|
|
34
41
|
rules: [nativeRule, artRule, htmlRule, jsRule, pyRule, pyRule2],
|
|
35
42
|
cache_root: './cache'.fullname(),
|
|
@@ -48,12 +55,17 @@ class Tpl {
|
|
|
48
55
|
|
|
49
56
|
// 修改配置
|
|
50
57
|
this.set_config(config);
|
|
51
|
-
|
|
58
|
+
|
|
52
59
|
/**
|
|
53
|
-
*
|
|
60
|
+
* 模板根目录
|
|
54
61
|
*/
|
|
55
62
|
this.dir = "./template/";
|
|
56
|
-
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 当前主题
|
|
66
|
+
*/
|
|
67
|
+
this.current_theme = this.config.defualt_theme;
|
|
68
|
+
|
|
57
69
|
/**
|
|
58
70
|
* 错误提示
|
|
59
71
|
*/
|
|
@@ -103,6 +115,33 @@ Tpl.prototype.render_before = function(body, model, options) {
|
|
|
103
115
|
return ret;
|
|
104
116
|
};
|
|
105
117
|
|
|
118
|
+
/**
|
|
119
|
+
* 加载函数
|
|
120
|
+
* @param {Object} m 模型
|
|
121
|
+
*/
|
|
122
|
+
Tpl.prototype.loadFunc = function(m) {
|
|
123
|
+
var _this = this;
|
|
124
|
+
m.view = function(file, model, options) {
|
|
125
|
+
return _this.view(file, model, options);
|
|
126
|
+
}
|
|
127
|
+
m.render = function(body, model, options) {
|
|
128
|
+
return _this.render(body, model, options);
|
|
129
|
+
}
|
|
130
|
+
m.hook_action = function(name, content, ...args) {
|
|
131
|
+
if (!content) {
|
|
132
|
+
content = "";
|
|
133
|
+
}
|
|
134
|
+
return $.hook.runAction(name, content, ...args);
|
|
135
|
+
}
|
|
136
|
+
m.hook_filter = function(name, content, ...args) {
|
|
137
|
+
if (!content) {
|
|
138
|
+
content = "";
|
|
139
|
+
}
|
|
140
|
+
return $.hook.runFilter(name, content, ...args);
|
|
141
|
+
}
|
|
142
|
+
return m;
|
|
143
|
+
}
|
|
144
|
+
|
|
106
145
|
/**
|
|
107
146
|
* 渲染模板(主要)
|
|
108
147
|
* @param {String} body 模板
|
|
@@ -113,10 +152,12 @@ Tpl.prototype.render_before = function(body, model, options) {
|
|
|
113
152
|
Tpl.prototype.render_main = function(body, model, options) {
|
|
114
153
|
var ret = "";
|
|
115
154
|
if (body) {
|
|
116
|
-
|
|
155
|
+
var m = Object.assign({}, {
|
|
117
156
|
viewBag: this.viewBag,
|
|
118
157
|
globalBag: $.globalBag
|
|
119
|
-
}, model)
|
|
158
|
+
}, model);
|
|
159
|
+
this.loadFunc(m);
|
|
160
|
+
ret = template.render(body, m, options);
|
|
120
161
|
}
|
|
121
162
|
return ret;
|
|
122
163
|
};
|
|
@@ -184,8 +225,18 @@ Tpl.prototype.view = function(file, model, options) {
|
|
|
184
225
|
if (file.replace('./', '').indexOf('.') === -1 && !file.endWith(this.config.extname)) {
|
|
185
226
|
file += this.config.extname;
|
|
186
227
|
}
|
|
187
|
-
var f = file.fullname(this.dir);
|
|
188
|
-
if(!f.hasFile()){
|
|
228
|
+
var f = file.fullname(this.dir + this.current_theme);
|
|
229
|
+
if (!f.hasFile()) {
|
|
230
|
+
f = file.fullname(this.dir + this.config.defualt_theme);
|
|
231
|
+
if (!f.hasFile()) {
|
|
232
|
+
f = file.fullname(this.defualt_dir + this.config.current_theme);
|
|
233
|
+
if (!f.hasFile()) {
|
|
234
|
+
f = file.fullname(this.defualt_dir + this.config.defualt_theme);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (!f.hasFile()) {
|
|
189
240
|
this.error = {
|
|
190
241
|
code: 10000,
|
|
191
242
|
message: f + "文件不存在!"
|
|
@@ -194,8 +245,7 @@ Tpl.prototype.view = function(file, model, options) {
|
|
|
194
245
|
var body = f.loadText();
|
|
195
246
|
if (body) {
|
|
196
247
|
return this.render(body, model, options);
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
248
|
+
} else {
|
|
199
249
|
return "";
|
|
200
250
|
}
|
|
201
251
|
};
|
|
@@ -256,4 +306,4 @@ if (global.$) {
|
|
|
256
306
|
$.tpl = new Tpl();
|
|
257
307
|
}
|
|
258
308
|
|
|
259
|
-
module.exports = Tpl;
|
|
309
|
+
module.exports = Tpl;
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -44,6 +44,24 @@ async function test() {
|
|
|
44
44
|
|
|
45
45
|
// 设置实例化视图变量
|
|
46
46
|
tpl.viewBag.name = "系统名称";
|
|
47
|
+
tpl.current_theme = "game";
|
|
48
|
+
tpl.dir = "";
|
|
49
|
+
|
|
50
|
+
$.hook.addAction('test', (ret) => {
|
|
51
|
+
return "<div>你好吗?</div>";
|
|
52
|
+
}, 2, "测试动作2");
|
|
53
|
+
|
|
54
|
+
$.hook.addAction('test', (ret) => {
|
|
55
|
+
return "<div>你好!</div>";
|
|
56
|
+
}, 1, "测试动作1");
|
|
57
|
+
|
|
58
|
+
$.hook.addFilter('test', (ret) => {
|
|
59
|
+
return ret.replace("你", "您");
|
|
60
|
+
}, 1, "测试过滤");
|
|
61
|
+
|
|
62
|
+
// $.hook.delAction('test', "测试动作1");
|
|
63
|
+
// $.hook.delFilter('test', "测试过滤");
|
|
64
|
+
|
|
47
65
|
// 渲染视图(并缓存)
|
|
48
66
|
var body = tpl.view("./test", { arr: [123, 234] }, {
|
|
49
67
|
// 是否读取缓存文件, 如果为false, 但有cache_filename, 表示只保存缓存,但不读取
|
|
File without changes
|