mm_tpl 2.1.8 → 2.2.0

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/hook.js CHANGED
@@ -1,5 +1,10 @@
1
+ const {
2
+ types
3
+ } = require("node:util");
4
+
1
5
  class Hook {
2
6
  constructor() {
7
+ this.dict_func = {};
3
8
  this.dict_action = {};
4
9
  this.dict_filter = {};
5
10
  }
@@ -15,6 +20,27 @@ Hook.prototype.sort = function(list) {
15
20
  });
16
21
  }
17
22
 
23
+ /**
24
+ * 添加函数钩子
25
+ * @param {String} name 钩子名称
26
+ * @param {Function} func 钩子函数
27
+ * @param {Number} sort 执行顺序
28
+ * @param {String} alias 函数别名,用于删除
29
+ */
30
+ Hook.prototype.addFunc = function(name, func, sort, alias) {
31
+ var obj = {
32
+ func,
33
+ sort,
34
+ alias
35
+ }
36
+ var arr = this.dict_func[name];
37
+ if (!arr) {
38
+ arr = [];
39
+ }
40
+ arr.push(obj);
41
+ this.dict_func[name] = this.sort(arr);
42
+ }
43
+
18
44
  /**
19
45
  * 添加动作钩子
20
46
  * @param {String} name 钩子名称
@@ -57,6 +83,21 @@ Hook.prototype.addFilter = function(name, func, sort, alias) {
57
83
  this.dict_filter[name] = this.sort(arr);
58
84
  }
59
85
 
86
+
87
+ /**
88
+ * 删除函数钩子
89
+ * @param {String} name 钩子名称
90
+ * @param {String} alias 函数别名
91
+ */
92
+ Hook.prototype.delFunc = function(name, alias) {
93
+ var arr = this.dict_func[name];
94
+ if (arr) {
95
+ arr.del({
96
+ alias
97
+ });
98
+ }
99
+ }
100
+
60
101
  /**
61
102
  * 删除动作钩子
62
103
  * @param {String} name 钩子名称
@@ -88,16 +129,21 @@ Hook.prototype.delFilter = function(name, alias = "") {
88
129
  /**
89
130
  * 运行动作钩子
90
131
  * @param {String} name 钩子名称
91
- * @param {Object} ret 结果
132
+ * @param {Object} model 模型
92
133
  * @param {Object} args 附加参数
93
134
  */
94
- Hook.prototype.runAction = function(name, ret, ...args) {
135
+ Hook.prototype.runAction = function(name, model, ...args) {
136
+ var ret = "";
95
137
  var dict = this.dict_action;
96
138
  var list = dict[name];
97
139
  if (list) {
98
140
  for (var i = 0; i < list.length; i++) {
99
141
  var o = list[i];
100
- ret += o.func(ret, ...args);
142
+ try {
143
+ ret += o.func(model, ...args);
144
+ } catch (err) {
145
+ console.error(ret);
146
+ }
101
147
  }
102
148
  }
103
149
  return ret;
@@ -107,15 +153,47 @@ Hook.prototype.runAction = function(name, ret, ...args) {
107
153
  * 运行过滤钩子
108
154
  * @param {String} name 钩子名称
109
155
  * @param {Object} ret 结果
156
+ * @param {Object} model 模型
110
157
  * @param {Object} args 附加参数
111
158
  */
112
- Hook.prototype.runFilter = function(name, ret, ...args) {
159
+ Hook.prototype.runFilter = function(name, ret, model, ...args) {
113
160
  var dict = this.dict_filter;
114
161
  var list = dict[name];
115
162
  if (list) {
116
163
  for (var i = 0; i < list.length; i++) {
117
164
  var o = list[i];
118
- ret = o.func(ret, ...args);
165
+ try {
166
+ ret = o.func(ret, model, ...args);
167
+ } catch (err) {
168
+ console.error(ret);
169
+ }
170
+ }
171
+ }
172
+ return ret;
173
+ }
174
+
175
+ /**
176
+ * 运行函数钩子
177
+ * @param {String} name 钩子名称
178
+ * @param {Object} model 模型
179
+ * @param {Object} req 请求参数
180
+ * @param {Object} args 附加参数
181
+ */
182
+ Hook.prototype.runFunc = async function(name, model, req, ...args) {
183
+ var ret;
184
+ var dict = this.dict_func;
185
+ var list = dict[name];
186
+ if (list) {
187
+ for (var i = 0; i < list.length; i++) {
188
+ var o = list[i];
189
+ try {
190
+ ret = o.func(model, req, ...args);
191
+ if (ret) {
192
+ ret = await ret;
193
+ }
194
+ } catch (err) {
195
+ console.error(ret);
196
+ }
119
197
  }
120
198
  }
121
199
  return ret;
package/index.js CHANGED
@@ -141,7 +141,7 @@ Tpl.prototype.loadFunc = function(m) {
141
141
  return _this.render(body, model, options);
142
142
  }
143
143
  m.hook_action = function(name, ...args) {
144
- return $.hook.runAction(name, "", m, ...args);
144
+ return $.hook.runAction(name, m, ...args);
145
145
  }
146
146
  m.hook_filter = function(name, content, ...args) {
147
147
  if (!content) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_tpl",
3
- "version": "2.1.8",
3
+ "version": "2.2.0",
4
4
  "description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,4 +1,4 @@
1
- <div class="bar_user">${@hook_filter('test', hook_action('test'))}</div>
1
+ <div class="bar_user">${@hook_filter('test', hook_action('test', '好的'), '真的')}</div>
2
2
  <div class="bar">导航栏</div>
3
3
  <style>
4
4
  .bar {
package/test.js CHANGED
@@ -11,13 +11,13 @@ const Tpl = require('./index.js');
11
11
  // async function test() {
12
12
  // var tpl = new Tpl();
13
13
  // // var body = tpl.render("<a>${loop arr value idx} ${idx} ${ value } ${/loop} + 123123</a>", { arr: [123,234] });
14
-
14
+
15
15
  // var body = tpl.render("<a>${ loop obj value key } ${ key } ${ value } ${/loop} + 123123</a>", { obj: { a: 123, b: 467 } });
16
-
16
+
17
17
  // // var body = tpl.render("<a><!--[ for(var i = 0; i < arr.length; i++){ ]--> ${ arr[i] } <!--[ } ]--> + 123123</a>", {
18
18
  // // arr: [123, 234]
19
19
  // // });
20
-
20
+
21
21
  // // var body = tpl.render("<a>${ each arr } ${ arr[$index] } ${/each} + 123123</a>", { arr: [123,234] });
22
22
  // // var view = tpl.render("<a>${ each arr } ${ $value } ${/each} + 123123</a>", { arr: [123,234] });
23
23
  // console.log(body);
@@ -27,10 +27,10 @@ const Tpl = require('./index.js');
27
27
  // var tpl = new Tpl();
28
28
  // // 设置全局变量
29
29
  // $.globalBag.name = "超级美眉Art模板引擎";
30
-
30
+
31
31
  // // 设置实例化视图变量
32
32
  // tpl.viewBag.name = "系统名称";
33
-
33
+
34
34
  // // 渲染视图
35
35
  // var body = tpl.view("./test.html", { arr: [123,234] });
36
36
  // console.log(body);
@@ -38,42 +38,55 @@ const Tpl = require('./index.js');
38
38
 
39
39
  async function test() {
40
40
  var tpl = new Tpl();
41
-
41
+
42
42
  // 设置全局变量
43
43
  $.globalBag.name = "超级美眉Art模板引擎";
44
-
44
+
45
45
  // 设置实例化视图变量
46
46
  tpl.viewBag.name = "系统名称";
47
47
  tpl.current_theme = "game";
48
48
  tpl.dir = "";
49
+
50
+ $.hook.addFunc('list', async function(viewBag, req, type) {
51
+ console.log(viewBag, req, type);
52
+ });
49
53
 
50
- $.hook.addAction('test', function(ret, m) {
51
- console.log(m);
54
+ await $.hook.runFunc('list', tpl.viewBag, {
55
+ query: {
56
+ a: 1,
57
+ b: 2
58
+ }
59
+ }, 'article');
60
+
61
+ $.hook.addAction('test', function(m, param) {
62
+ // console.log(param, m);
52
63
  return "<div>你好吗?</div>";
53
64
  }, 2, "测试动作2");
54
-
55
- $.hook.addAction('test', function(ret) {
65
+
66
+ $.hook.addAction('test', function(m, param) {
56
67
  return "<div>你好!</div>";
57
68
  }, 1, "测试动作1");
58
-
59
- $.hook.addFilter('test', function(ret, m) {
60
- console.log(m);
69
+
70
+ $.hook.addFilter('test', function(ret, m, param) {
71
+ // console.log(ret, param, m);
61
72
  return ret.replace("你", "您");
62
73
  }, 1, "测试过滤");
63
-
74
+
64
75
  // $.hook.delAction('test', "测试动作1");
65
76
  // $.hook.delFilter('test', "测试过滤");
66
-
77
+
67
78
  // 渲染视图(并缓存)
68
- var body = tpl.view("./test", { arr: [123, 234] }, {
79
+ var body = tpl.view("./test", {
80
+ arr: [123, 234]
81
+ }, {
69
82
  // 是否读取缓存文件, 如果为false, 但有cache_filename, 表示只保存缓存,但不读取
70
83
  cache: false,
71
84
  // 缓存文件名
72
85
  cache_filename: "./test"
73
86
  });
74
-
87
+
75
88
  // 删除指定目录的缓存文件
76
89
  // tpl.clear_cache('./mall');
77
90
  console.log(body);
78
91
  }
79
- test();
92
+ test();