mm_tpl 2.3.0 → 2.3.2
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 +5 -4
- package/index.js +41 -1
- package/package.json +1 -1
- package/template/{defualt → default}/test.html +3 -3
- package/template/diy/ad.html +2 -2
- package/test.js +67 -65
package/cache/test.cache.html
CHANGED
|
@@ -9,16 +9,16 @@
|
|
|
9
9
|
<p>您真的是人才!</p>
|
|
10
10
|
<p>您真的是人才!</p>
|
|
11
11
|
<ul>
|
|
12
|
-
<li
|
|
13
|
-
<li
|
|
12
|
+
<li>\123</li>
|
|
13
|
+
<li>\234</li>
|
|
14
14
|
</ul>
|
|
15
15
|
<ul>
|
|
16
16
|
<li>123</li>
|
|
17
17
|
<li>234</li>
|
|
18
18
|
</ul>
|
|
19
19
|
<ul>
|
|
20
|
-
<li
|
|
21
|
-
<li
|
|
20
|
+
<li>\123</li>
|
|
21
|
+
<li>\234</li>
|
|
22
22
|
</ul>
|
|
23
23
|
<script type="text/javascript">
|
|
24
24
|
var a_0 =123;
|
|
@@ -27,3 +27,4 @@
|
|
|
27
27
|
123123'priceNIHAO'#计算
|
|
28
28
|
</body>
|
|
29
29
|
</html>
|
|
30
|
+
$
|
package/index.js
CHANGED
|
@@ -40,7 +40,8 @@ class Tpl {
|
|
|
40
40
|
extname: ".html",
|
|
41
41
|
rules: [nativeRule, artRule, htmlRule, jsRule, pyRule, pyRule2],
|
|
42
42
|
cache_root: './cache'.fullname(),
|
|
43
|
-
cache_extname: '.cache.html'
|
|
43
|
+
cache_extname: '.cache.html',
|
|
44
|
+
change_str: ["\\$", "\\\\", "\\<", "\\>"],
|
|
44
45
|
};
|
|
45
46
|
|
|
46
47
|
/**
|
|
@@ -71,6 +72,12 @@ class Tpl {
|
|
|
71
72
|
*/
|
|
72
73
|
this.error = null;
|
|
73
74
|
|
|
75
|
+
this.regex_in = [];
|
|
76
|
+
this.str_in = [];
|
|
77
|
+
|
|
78
|
+
this.regex_out = [];
|
|
79
|
+
this.str_out = [];
|
|
80
|
+
|
|
74
81
|
/**
|
|
75
82
|
* 初始化
|
|
76
83
|
*/
|
|
@@ -112,6 +119,37 @@ Tpl.prototype.runFunc = function(name, req, ...args) {
|
|
|
112
119
|
*/
|
|
113
120
|
Tpl.prototype.init = function(config) {
|
|
114
121
|
this.set_config(config);
|
|
122
|
+
var lt = this.config.change_str;
|
|
123
|
+
for (var i = 0; i < lt.length; i++) {
|
|
124
|
+
var str = lt[i];
|
|
125
|
+
var rx = str.replace(/\\/g, "\\\\").replace("$", "\\$").replace("[", "\\[").replace("]", "\\]");
|
|
126
|
+
this.regex_in.push(new RegExp(rx, "gi"));
|
|
127
|
+
str = str.replace("\\", "");
|
|
128
|
+
this.str_in.push(str + "··");
|
|
129
|
+
this.regex_out.push(new RegExp(rx.replace(/\\/g, "") + "··", "gi"));
|
|
130
|
+
this.str_out.push(str.replace("\\", ""));
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* 转译字符
|
|
136
|
+
* @param {String} body 字符串
|
|
137
|
+
* @param {Boolean} restore 是否还原
|
|
138
|
+
*/
|
|
139
|
+
Tpl.prototype.change = function(body, restore) {
|
|
140
|
+
if (!body) {
|
|
141
|
+
return body
|
|
142
|
+
}
|
|
143
|
+
if (restore) {
|
|
144
|
+
for (var i = 0; i < this.regex_out.length; i++) {
|
|
145
|
+
body = body.replace(this.regex_out[i], this.str_out[i]);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
for (var i = 0; i < this.regex_in.length; i++) {
|
|
149
|
+
body = body.replace(this.regex_in[i], this.str_in[i]);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return body;
|
|
115
153
|
}
|
|
116
154
|
|
|
117
155
|
/**
|
|
@@ -215,6 +253,7 @@ Tpl.prototype.render_after = function(body, model, options) {
|
|
|
215
253
|
* @return {String} view 渲染后的视图
|
|
216
254
|
*/
|
|
217
255
|
Tpl.prototype.render = function(body, model, options) {
|
|
256
|
+
body = this.change(body);
|
|
218
257
|
var ret = this.render_before(body, model, options);
|
|
219
258
|
if (!ret) {
|
|
220
259
|
ret = this.render_main(body, model, options);
|
|
@@ -222,6 +261,7 @@ Tpl.prototype.render = function(body, model, options) {
|
|
|
222
261
|
ret = this.render_after(ret, model, options);
|
|
223
262
|
}
|
|
224
263
|
}
|
|
264
|
+
ret = this.change(ret, true);
|
|
225
265
|
return ret;
|
|
226
266
|
};
|
|
227
267
|
|
package/package.json
CHANGED
|
@@ -8,13 +8,13 @@
|
|
|
8
8
|
<h5>${viewBag.name}</h5>
|
|
9
9
|
<ul>
|
|
10
10
|
${loop arr value idx}
|
|
11
|
-
<li
|
|
11
|
+
<li>\${ value }</li>
|
|
12
12
|
${/loop}
|
|
13
13
|
</ul>
|
|
14
14
|
<ul>
|
|
15
|
-
|
|
15
|
+
\<!--[ for(var i = 0; i < arr.length; i++) { ]-->
|
|
16
16
|
<li>${ arr[i] }</li>
|
|
17
|
-
|
|
17
|
+
\<!--[ } ]-->
|
|
18
18
|
</ul>
|
|
19
19
|
<ul>
|
|
20
20
|
<!--{loop arr value idx}-->
|
package/template/diy/ad.html
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
<p><!--{@hook_filter('test', '你真的是人才!', {key: '123'})}--></p>
|
|
11
11
|
<ul>
|
|
12
12
|
${loop arr value idx}
|
|
13
|
-
<li
|
|
13
|
+
<li>\${ value }</li>
|
|
14
14
|
${/loop}
|
|
15
15
|
</ul>
|
|
16
16
|
<ul>
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
</ul>
|
|
21
21
|
<ul>
|
|
22
22
|
<!--{loop arr value idx}-->
|
|
23
|
-
<li
|
|
23
|
+
<li>\${ value }</li>
|
|
24
24
|
<!--{/loop}-->
|
|
25
25
|
</ul>
|
|
26
26
|
<script type="text/javascript">
|
package/test.js
CHANGED
|
@@ -23,79 +23,81 @@ const Tpl = require('./index.js');
|
|
|
23
23
|
// console.log(body);
|
|
24
24
|
// }
|
|
25
25
|
|
|
26
|
+
async function test() {
|
|
27
|
+
console.log("测试开始")
|
|
28
|
+
var tpl = new Tpl();
|
|
29
|
+
// 设置全局变量
|
|
30
|
+
$.globalBag.name = "超级美眉Art模板引擎";
|
|
31
|
+
|
|
32
|
+
// 设置实例化视图变量
|
|
33
|
+
tpl.viewBag.name = "系统名称";
|
|
34
|
+
|
|
35
|
+
// 渲染视图
|
|
36
|
+
var body = tpl.view("./test.html", { arr: [123,234] });
|
|
37
|
+
console.log(tpl.error);
|
|
38
|
+
console.log(body);
|
|
39
|
+
}
|
|
40
|
+
|
|
26
41
|
// async function test() {
|
|
27
42
|
// var tpl = new Tpl();
|
|
43
|
+
|
|
28
44
|
// // 设置全局变量
|
|
29
45
|
// $.globalBag.name = "超级美眉Art模板引擎";
|
|
30
46
|
|
|
31
47
|
// // 设置实例化视图变量
|
|
32
48
|
// tpl.viewBag.name = "系统名称";
|
|
49
|
+
// tpl.current_theme = "game";
|
|
50
|
+
// tpl.dir = "";
|
|
51
|
+
|
|
52
|
+
// $.hook.addFunc('list', async function(m, req, type) {
|
|
53
|
+
// var query = req.query;
|
|
54
|
+
// if (query.user_id) {
|
|
55
|
+
// m.user_id = query.user_id;
|
|
56
|
+
// }
|
|
57
|
+
// if (query.project_id) {
|
|
58
|
+
// m.project_id = query.project_id;
|
|
59
|
+
// }
|
|
60
|
+
// console.log("列表", m);
|
|
61
|
+
// });
|
|
33
62
|
|
|
34
|
-
//
|
|
35
|
-
//
|
|
63
|
+
// $.hook.addAction('test', function aa(m, param) {
|
|
64
|
+
// // console.log(param, m);
|
|
65
|
+
// return "<div>你好吗?</div>";
|
|
66
|
+
// }, 2);
|
|
67
|
+
|
|
68
|
+
// $.hook.addAction('test', function(m, param) {
|
|
69
|
+
// return "<div>你好!</div>";
|
|
70
|
+
// }, 1, "测试动作1");
|
|
71
|
+
|
|
72
|
+
// $.hook.addFilter('test', function(m, ret, param) {
|
|
73
|
+
// console.log("测试", ret);
|
|
74
|
+
// // console.log(param, ret, m);
|
|
75
|
+
// return ret.replace("你", "您");
|
|
76
|
+
// }, 1, "测试过滤");
|
|
77
|
+
|
|
78
|
+
// $.hook.delAction('test', "aa");
|
|
79
|
+
// $.hook.delAction('test', "测试动作1");
|
|
80
|
+
// // $.hook.delFilter('test', "测试过滤");
|
|
81
|
+
|
|
82
|
+
// await tpl.runFunc('list', {
|
|
83
|
+
// query: {
|
|
84
|
+
// user_id: 1,
|
|
85
|
+
// project_id: 2
|
|
86
|
+
// }
|
|
87
|
+
// }, 'article');
|
|
88
|
+
|
|
89
|
+
// // 渲染视图(并缓存)
|
|
90
|
+
// var body = tpl.view("./ad", {
|
|
91
|
+
// arr: [123, 234],
|
|
92
|
+
// message: "hello world!"
|
|
93
|
+
// }, {
|
|
94
|
+
// // 是否读取缓存文件, 如果为false, 但有cache_filename, 表示只保存缓存,但不读取
|
|
95
|
+
// cache: false,
|
|
96
|
+
// // 缓存文件名
|
|
97
|
+
// cache_filename: "./test"
|
|
98
|
+
// });
|
|
99
|
+
// // 删除指定目录的缓存文件
|
|
100
|
+
// // tpl.clear_cache('./mall');
|
|
36
101
|
// console.log(body);
|
|
37
102
|
// }
|
|
38
|
-
|
|
39
|
-
async function test() {
|
|
40
|
-
var tpl = new Tpl();
|
|
41
|
-
|
|
42
|
-
// 设置全局变量
|
|
43
|
-
$.globalBag.name = "超级美眉Art模板引擎";
|
|
44
|
-
|
|
45
|
-
// 设置实例化视图变量
|
|
46
|
-
tpl.viewBag.name = "系统名称";
|
|
47
|
-
tpl.current_theme = "game";
|
|
48
|
-
tpl.dir = "";
|
|
49
|
-
|
|
50
|
-
$.hook.addFunc('list', async function(m, req, type) {
|
|
51
|
-
var query = req.query;
|
|
52
|
-
if (query.user_id) {
|
|
53
|
-
m.user_id = query.user_id;
|
|
54
|
-
}
|
|
55
|
-
if (query.project_id) {
|
|
56
|
-
m.project_id = query.project_id;
|
|
57
|
-
}
|
|
58
|
-
console.log("列表", m);
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
$.hook.addAction('test', function aa(m, param) {
|
|
62
|
-
// console.log(param, m);
|
|
63
|
-
return "<div>你好吗?</div>";
|
|
64
|
-
}, 2);
|
|
65
|
-
|
|
66
|
-
$.hook.addAction('test', function(m, param) {
|
|
67
|
-
return "<div>你好!</div>";
|
|
68
|
-
}, 1, "测试动作1");
|
|
69
|
-
|
|
70
|
-
$.hook.addFilter('test', function(m, ret, param) {
|
|
71
|
-
console.log("测试", ret);
|
|
72
|
-
// console.log(param, ret, m);
|
|
73
|
-
return ret.replace("你", "您");
|
|
74
|
-
}, 1, "测试过滤");
|
|
75
|
-
|
|
76
|
-
$.hook.delAction('test', "aa");
|
|
77
|
-
$.hook.delAction('test', "测试动作1");
|
|
78
|
-
// $.hook.delFilter('test', "测试过滤");
|
|
79
|
-
|
|
80
|
-
await tpl.runFunc('list', {
|
|
81
|
-
query: {
|
|
82
|
-
user_id: 1,
|
|
83
|
-
project_id: 2
|
|
84
|
-
}
|
|
85
|
-
}, 'article');
|
|
86
|
-
|
|
87
|
-
// 渲染视图(并缓存)
|
|
88
|
-
var body = tpl.view("./ad", {
|
|
89
|
-
arr: [123, 234],
|
|
90
|
-
message: "hello world!"
|
|
91
|
-
}, {
|
|
92
|
-
// 是否读取缓存文件, 如果为false, 但有cache_filename, 表示只保存缓存,但不读取
|
|
93
|
-
cache: false,
|
|
94
|
-
// 缓存文件名
|
|
95
|
-
cache_filename: "./test"
|
|
96
|
-
});
|
|
97
|
-
// 删除指定目录的缓存文件
|
|
98
|
-
// tpl.clear_cache('./mall');
|
|
99
|
-
console.log(body);
|
|
100
|
-
}
|
|
101
103
|
test();
|