mm_tpl 2.1.4 → 2.1.6
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 +73 -10
- package/package.json +2 -2
- 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,16 +55,26 @@ 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
|
*/
|
|
60
72
|
this.error = null;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 初始化
|
|
76
|
+
*/
|
|
77
|
+
this.init(config);
|
|
61
78
|
}
|
|
62
79
|
}
|
|
63
80
|
|
|
@@ -78,6 +95,14 @@ Tpl.prototype.set_config = function(config) {
|
|
|
78
95
|
}
|
|
79
96
|
};
|
|
80
97
|
|
|
98
|
+
/**
|
|
99
|
+
* 初始化
|
|
100
|
+
* @param {Object} config 配置参数
|
|
101
|
+
*/
|
|
102
|
+
Tpl.prototype.init = function(config) {
|
|
103
|
+
this.set_config(config);
|
|
104
|
+
}
|
|
105
|
+
|
|
81
106
|
/**
|
|
82
107
|
* 渲染模板(之前)
|
|
83
108
|
* @param {String} body 模板
|
|
@@ -103,6 +128,33 @@ Tpl.prototype.render_before = function(body, model, options) {
|
|
|
103
128
|
return ret;
|
|
104
129
|
};
|
|
105
130
|
|
|
131
|
+
/**
|
|
132
|
+
* 加载函数
|
|
133
|
+
* @param {Object} m 模型
|
|
134
|
+
*/
|
|
135
|
+
Tpl.prototype.loadFunc = function(m) {
|
|
136
|
+
var _this = this;
|
|
137
|
+
m.view = function(file, model, options) {
|
|
138
|
+
return _this.view(file, model, options);
|
|
139
|
+
}
|
|
140
|
+
m.render = function(body, model, options) {
|
|
141
|
+
return _this.render(body, model, options);
|
|
142
|
+
}
|
|
143
|
+
m.hook_action = function(name, content, ...args) {
|
|
144
|
+
if (!content) {
|
|
145
|
+
content = "";
|
|
146
|
+
}
|
|
147
|
+
return $.hook.runAction(name, content, ...args);
|
|
148
|
+
}
|
|
149
|
+
m.hook_filter = function(name, content, ...args) {
|
|
150
|
+
if (!content) {
|
|
151
|
+
content = "";
|
|
152
|
+
}
|
|
153
|
+
return $.hook.runFilter(name, content, ...args);
|
|
154
|
+
}
|
|
155
|
+
return m;
|
|
156
|
+
}
|
|
157
|
+
|
|
106
158
|
/**
|
|
107
159
|
* 渲染模板(主要)
|
|
108
160
|
* @param {String} body 模板
|
|
@@ -113,10 +165,12 @@ Tpl.prototype.render_before = function(body, model, options) {
|
|
|
113
165
|
Tpl.prototype.render_main = function(body, model, options) {
|
|
114
166
|
var ret = "";
|
|
115
167
|
if (body) {
|
|
116
|
-
|
|
168
|
+
var m = Object.assign({}, {
|
|
117
169
|
viewBag: this.viewBag,
|
|
118
170
|
globalBag: $.globalBag
|
|
119
|
-
}, model)
|
|
171
|
+
}, model);
|
|
172
|
+
this.loadFunc(m);
|
|
173
|
+
ret = template.render(body, m, options);
|
|
120
174
|
}
|
|
121
175
|
return ret;
|
|
122
176
|
};
|
|
@@ -184,8 +238,18 @@ Tpl.prototype.view = function(file, model, options) {
|
|
|
184
238
|
if (file.replace('./', '').indexOf('.') === -1 && !file.endWith(this.config.extname)) {
|
|
185
239
|
file += this.config.extname;
|
|
186
240
|
}
|
|
187
|
-
var f = file.fullname(this.dir);
|
|
188
|
-
if(!f.hasFile()){
|
|
241
|
+
var f = file.fullname(this.dir + this.current_theme);
|
|
242
|
+
if (!f.hasFile()) {
|
|
243
|
+
f = file.fullname(this.dir + this.config.defualt_theme);
|
|
244
|
+
if (!f.hasFile()) {
|
|
245
|
+
f = file.fullname(this.defualt_dir + this.config.current_theme);
|
|
246
|
+
if (!f.hasFile()) {
|
|
247
|
+
f = file.fullname(this.defualt_dir + this.config.defualt_theme);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (!f.hasFile()) {
|
|
189
253
|
this.error = {
|
|
190
254
|
code: 10000,
|
|
191
255
|
message: f + "文件不存在!"
|
|
@@ -194,8 +258,7 @@ Tpl.prototype.view = function(file, model, options) {
|
|
|
194
258
|
var body = f.loadText();
|
|
195
259
|
if (body) {
|
|
196
260
|
return this.render(body, model, options);
|
|
197
|
-
}
|
|
198
|
-
else {
|
|
261
|
+
} else {
|
|
199
262
|
return "";
|
|
200
263
|
}
|
|
201
264
|
};
|
|
@@ -256,4 +319,4 @@ if (global.$) {
|
|
|
256
319
|
$.tpl = new Tpl();
|
|
257
320
|
}
|
|
258
321
|
|
|
259
|
-
module.exports = Tpl;
|
|
322
|
+
module.exports = Tpl;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_tpl",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,6 +27,6 @@
|
|
|
27
27
|
"homepage": "https://github.com/qiuwenwu/mm_tpl#readme",
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"art-template": "^4.13.2",
|
|
30
|
-
"mm_expand": "^1.5.
|
|
30
|
+
"mm_expand": "^1.5.5"
|
|
31
31
|
}
|
|
32
32
|
}
|
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
|