mm_tpl 2.4.2 → 2.4.4
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/README.md +47 -2
- package/cache/test.cache.html +1 -1
- package/hook.js +3 -3
- package/index.js +7 -7
- package/package.json +6 -6
- package/template/defualt/test.html +36 -0
- package/test.js +8 -8
- package/tpl_rule.js +1 -1
package/README.md
CHANGED
|
@@ -61,6 +61,28 @@ const objectResult = tpl.render(`
|
|
|
61
61
|
`, {
|
|
62
62
|
obj: { name: "张三", age: 25 }
|
|
63
63
|
});
|
|
64
|
+
|
|
65
|
+
// 传统for循环
|
|
66
|
+
const forResult = tpl.render(`
|
|
67
|
+
<ul>
|
|
68
|
+
<!--[ for(var i = 0; i < arr.length; i++){ ]-->
|
|
69
|
+
<li>${arr[i]}</li>
|
|
70
|
+
<!--[ } ]-->
|
|
71
|
+
</ul>
|
|
72
|
+
`, {
|
|
73
|
+
arr: [123, 234]
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// each循环
|
|
77
|
+
const eachResult = tpl.render(`
|
|
78
|
+
<ul>
|
|
79
|
+
${each arr}
|
|
80
|
+
<li>${$value}</li>
|
|
81
|
+
${/each}
|
|
82
|
+
</ul>
|
|
83
|
+
`, {
|
|
84
|
+
arr: [123, 234]
|
|
85
|
+
});
|
|
64
86
|
```
|
|
65
87
|
|
|
66
88
|
### 4. 视图渲染
|
|
@@ -99,6 +121,10 @@ tpl.clear_cache('./views/mall');
|
|
|
99
121
|
```javascript
|
|
100
122
|
$.hook.addFunc('list', async function(viewBag, req, type) {
|
|
101
123
|
// 处理列表逻辑
|
|
124
|
+
var query = req.query;
|
|
125
|
+
if (query.user_id) {
|
|
126
|
+
viewBag.user_id = query.user_id;
|
|
127
|
+
}
|
|
102
128
|
return someData;
|
|
103
129
|
});
|
|
104
130
|
```
|
|
@@ -117,7 +143,7 @@ $.hook.addFilter('content', function(viewBag, content, param) {
|
|
|
117
143
|
}, 1, "contentFilter");
|
|
118
144
|
```
|
|
119
145
|
|
|
120
|
-
####
|
|
146
|
+
#### 在模板中使用钩子
|
|
121
147
|
```javascript
|
|
122
148
|
// 运行函数钩子
|
|
123
149
|
await tpl.runFunc('list', {
|
|
@@ -151,16 +177,35 @@ tpl.current_theme = "dark";
|
|
|
151
177
|
```javascript
|
|
152
178
|
const html = tpl.view("./template.html", data);
|
|
153
179
|
if (tpl.error) {
|
|
154
|
-
|
|
180
|
+
$.log.error("渲染错误:", tpl.error);
|
|
155
181
|
}
|
|
156
182
|
```
|
|
157
183
|
|
|
184
|
+
### 4. 视图背包操作
|
|
185
|
+
```javascript
|
|
186
|
+
// 设置视图变量
|
|
187
|
+
tpl.set({
|
|
188
|
+
title: "页面标题",
|
|
189
|
+
keywords: "关键词"
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// 获取视图变量
|
|
193
|
+
const title = tpl.get("title");
|
|
194
|
+
|
|
195
|
+
// 删除视图变量
|
|
196
|
+
tpl.del("title");
|
|
197
|
+
// 或批量删除
|
|
198
|
+
tpl.del(["title", "keywords"]);
|
|
199
|
+
```
|
|
200
|
+
|
|
158
201
|
## 注意事项
|
|
159
202
|
|
|
160
203
|
1. 确保模板目录具有正确的读写权限
|
|
161
204
|
2. 使用缓存时注意及时清理过期缓存
|
|
162
205
|
3. 钩子的优先级数值越小,执行顺序越靠前
|
|
163
206
|
4. 建议在生产环境开启模板缓存以提高性能
|
|
207
|
+
5. 模板文件查找顺序:diy > current_theme > default_theme
|
|
208
|
+
6. 使用钩子系统时注意处理异步操作
|
|
164
209
|
|
|
165
210
|
## 许可证
|
|
166
211
|
|
package/cache/test.cache.html
CHANGED
package/hook.js
CHANGED
|
@@ -165,7 +165,7 @@ Hook.prototype.runAction = function(name, model, ...args) {
|
|
|
165
165
|
try {
|
|
166
166
|
ret += o.func(model, ...args);
|
|
167
167
|
} catch (err) {
|
|
168
|
-
|
|
168
|
+
$.log.error(ret);
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
171
|
}
|
|
@@ -188,7 +188,7 @@ Hook.prototype.runFilter = function(name, model, ret, ...args) {
|
|
|
188
188
|
try {
|
|
189
189
|
ret = o.func(model, ret, ...args);
|
|
190
190
|
} catch (err) {
|
|
191
|
-
|
|
191
|
+
$.log.error(ret);
|
|
192
192
|
}
|
|
193
193
|
}
|
|
194
194
|
}
|
|
@@ -215,7 +215,7 @@ Hook.prototype.runFunc = async function(name, model, req, ...args) {
|
|
|
215
215
|
ret = await ret;
|
|
216
216
|
}
|
|
217
217
|
} catch (err) {
|
|
218
|
-
|
|
218
|
+
$.log.error(ret);
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
}
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
require('
|
|
1
|
+
require('mm_logs');
|
|
2
2
|
const template = require('art-template');
|
|
3
3
|
const Hook = require('./hook.js');
|
|
4
4
|
const {
|
|
@@ -55,7 +55,7 @@ class Tpl {
|
|
|
55
55
|
this.viewBag = {};
|
|
56
56
|
|
|
57
57
|
// 修改配置
|
|
58
|
-
this.
|
|
58
|
+
this.setConfig(config);
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
61
|
* 模板根目录
|
|
@@ -89,7 +89,7 @@ class Tpl {
|
|
|
89
89
|
* 修改配置
|
|
90
90
|
* @param {Object} config 配置参数
|
|
91
91
|
*/
|
|
92
|
-
Tpl.prototype.
|
|
92
|
+
Tpl.prototype.setConfig = function(config) {
|
|
93
93
|
if (config) {
|
|
94
94
|
this.config = Object.assign(this.config, config);
|
|
95
95
|
}
|
|
@@ -125,7 +125,7 @@ Tpl.prototype.init_before = function(config) {}
|
|
|
125
125
|
* @param {Object} config 配置参数
|
|
126
126
|
*/
|
|
127
127
|
Tpl.prototype.init_main = function(config) {
|
|
128
|
-
this.
|
|
128
|
+
this.setConfig(config);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
/**
|
|
@@ -291,7 +291,7 @@ Tpl.prototype.render_after = function(body, model, options) {
|
|
|
291
291
|
* @param {Object} options 配置参数
|
|
292
292
|
* @return {String} view 渲染后的视图
|
|
293
293
|
*/
|
|
294
|
-
Tpl.prototype.
|
|
294
|
+
Tpl.prototype.renderSub = function(body, model, options) {
|
|
295
295
|
var ret = this.render_before(body, model, options);
|
|
296
296
|
if (!ret) {
|
|
297
297
|
ret = this.render_main(body, model, options);
|
|
@@ -311,7 +311,7 @@ Tpl.prototype.render_sub = function(body, model, options) {
|
|
|
311
311
|
*/
|
|
312
312
|
Tpl.prototype.render = function(body, model, options) {
|
|
313
313
|
body = this.change(body);
|
|
314
|
-
body = this.
|
|
314
|
+
body = this.renderSub(body, model, options);
|
|
315
315
|
return this.change(body, true);
|
|
316
316
|
};
|
|
317
317
|
|
|
@@ -362,7 +362,7 @@ Tpl.prototype.view = function(file, model, options) {
|
|
|
362
362
|
var body = f.loadText();
|
|
363
363
|
if (body) {
|
|
364
364
|
body = this.change(body);
|
|
365
|
-
body = this.
|
|
365
|
+
body = this.renderSub(body, model, options);
|
|
366
366
|
body = this.change(body, true);
|
|
367
367
|
return body;
|
|
368
368
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_tpl",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.4",
|
|
4
4
|
"description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"repository": {
|
|
11
11
|
"type": "git",
|
|
12
|
-
"url": "git+https://
|
|
12
|
+
"url": "git+https://gitee.com/qiuwenwu91/mm_tpl.git"
|
|
13
13
|
},
|
|
14
14
|
"keywords": [
|
|
15
15
|
"mvc",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"author": "邱文武",
|
|
24
24
|
"license": "ISC",
|
|
25
25
|
"bugs": {
|
|
26
|
-
"url": "https://
|
|
26
|
+
"url": "https://gitee.com/qiuwenwu91/mm_tpl/issues"
|
|
27
27
|
},
|
|
28
|
-
"homepage": "https://
|
|
28
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_tpl#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"art-template": "^4.13.
|
|
31
|
-
"
|
|
30
|
+
"art-template": "^4.13.4",
|
|
31
|
+
"mm_logs": "^1.5.3"
|
|
32
32
|
}
|
|
33
33
|
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<title>${globalBag.name}</title>
|
|
6
|
+
</head>
|
|
7
|
+
<body>
|
|
8
|
+
<h5>${viewBag.name}</h5>
|
|
9
|
+
<ul>
|
|
10
|
+
${loop arr value idx}
|
|
11
|
+
<li>${ value }</li>
|
|
12
|
+
${/loop}
|
|
13
|
+
</ul>
|
|
14
|
+
<ul>
|
|
15
|
+
<!--[ for(var i = 0; i < arr.length; i++) { ]-->
|
|
16
|
+
<li>${ arr[i] }</li>
|
|
17
|
+
<!--[ } ]-->
|
|
18
|
+
</ul>
|
|
19
|
+
<ul>
|
|
20
|
+
<!--{loop arr value idx}-->
|
|
21
|
+
<li>${ value }</li>
|
|
22
|
+
<!--{/loop}-->
|
|
23
|
+
</ul>
|
|
24
|
+
<script type="text/javascript">
|
|
25
|
+
/*[loop arr value idx]*/
|
|
26
|
+
var a_${idx} = ${value};
|
|
27
|
+
/*[/loop]*/
|
|
28
|
+
</script>
|
|
29
|
+
${viewBag.name}
|
|
30
|
+
## "计算" ##
|
|
31
|
+
123123'"""#
|
|
32
|
+
"price" + "NIHAO"
|
|
33
|
+
#"""'
|
|
34
|
+
## "#计算" ##
|
|
35
|
+
</body>
|
|
36
|
+
</html>
|
package/test.js
CHANGED
|
@@ -10,7 +10,7 @@ async function test() {
|
|
|
10
10
|
var body = tpl.render("<a>${test} + 123123</a>", {
|
|
11
11
|
test: 123
|
|
12
12
|
});
|
|
13
|
-
|
|
13
|
+
$.log.debug(body);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -34,7 +34,7 @@ async function test() {
|
|
|
34
34
|
// each循环示例
|
|
35
35
|
// var body = tpl.render("<a>${ each arr } ${ arr[$index] } ${/each} + 123123</a>", { arr: [123,234] });
|
|
36
36
|
// var view = tpl.render("<a>${ each arr } ${ $value } ${/each} + 123123</a>", { arr: [123,234] });
|
|
37
|
-
|
|
37
|
+
$.log.debug(body);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
@@ -43,7 +43,7 @@ async function test() {
|
|
|
43
43
|
* @async
|
|
44
44
|
*/
|
|
45
45
|
async function test() {
|
|
46
|
-
|
|
46
|
+
$.log.debug("测试开始")
|
|
47
47
|
var tpl = new Tpl();
|
|
48
48
|
// 设置全局变量
|
|
49
49
|
$.globalBag.name = "超级美眉Art模板引擎";
|
|
@@ -53,8 +53,8 @@ async function test() {
|
|
|
53
53
|
|
|
54
54
|
// 渲染视图
|
|
55
55
|
var body = tpl.view("./test.html", { arr: [123,234] });
|
|
56
|
-
|
|
57
|
-
|
|
56
|
+
$.log.debug(tpl.error);
|
|
57
|
+
$.log.debug(body);
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
/**
|
|
@@ -82,7 +82,7 @@ async function test() {
|
|
|
82
82
|
if (query.project_id) {
|
|
83
83
|
m.project_id = query.project_id;
|
|
84
84
|
}
|
|
85
|
-
|
|
85
|
+
$.log.debug("列表", m);
|
|
86
86
|
});
|
|
87
87
|
|
|
88
88
|
// 添加动作钩子
|
|
@@ -96,7 +96,7 @@ async function test() {
|
|
|
96
96
|
|
|
97
97
|
// 添加过滤器钩子
|
|
98
98
|
$.hook.addFilter('test', function(m, ret, param) {
|
|
99
|
-
|
|
99
|
+
$.log.debug("测试", ret);
|
|
100
100
|
return ret.replace("你", "您");
|
|
101
101
|
}, 1, "测试过滤");
|
|
102
102
|
|
|
@@ -125,7 +125,7 @@ async function test() {
|
|
|
125
125
|
});
|
|
126
126
|
// 删除指定目录下的缓存文件
|
|
127
127
|
// tpl.clear_cache('./mall');
|
|
128
|
-
|
|
128
|
+
$.log.debug(body);
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
// 执行测试
|
package/tpl_rule.js
CHANGED
|
@@ -51,7 +51,7 @@ var artRule = {
|
|
|
51
51
|
|
|
52
52
|
// 旧版语法升级提示
|
|
53
53
|
var warn = function warn(oldSyntax, newSyntax) {
|
|
54
|
-
|
|
54
|
+
$.log.warn((options.filename || 'anonymous') + ':' + (match.line + 1) + ':' + (match.start + 1) + '\n' + (
|
|
55
55
|
'Template upgrade: ${' + oldSyntax + '} -> ${' + newSyntax + '}'));
|
|
56
56
|
};
|
|
57
57
|
|