mm_tpl 2.4.4 → 2.4.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/README.md CHANGED
@@ -13,7 +13,7 @@ npm install mm_tpl
13
13
  ### 1. 初始化模板引擎
14
14
 
15
15
  ```javascript
16
- const Tpl = require('mm_tpl');
16
+ const {Tpl} = require('mm_tpl');
17
17
  const tpl = new Tpl({
18
18
  // 可选配置参数
19
19
  root: process.cwd(), // 根目录
@@ -83,6 +83,11 @@ const eachResult = tpl.render(`
83
83
  `, {
84
84
  arr: [123, 234]
85
85
  });
86
+ // 输出:
87
+ // <ul>
88
+ // <li>123</li>
89
+ // <li>234</li>
90
+ // </ul>
86
91
  ```
87
92
 
88
93
  ### 4. 视图渲染
@@ -98,6 +103,7 @@ tpl.viewBag.title = "首页";
98
103
  const html = tpl.view("./views/index.html", {
99
104
  user: { name: "张三" }
100
105
  });
106
+ // 输出: 渲染结果 <div>Hello World</div>
101
107
  ```
102
108
 
103
109
  ### 5. 缓存机制
@@ -6,8 +6,8 @@
6
6
  </head>
7
7
  <body>
8
8
  <h5>系统名称</h5>
9
- <p>您真的是人才!</p>
10
- <p>您真的是人才!</p>
9
+ <p>你真的是人才!</p>
10
+ <p>你真的是人才!</p>
11
11
  <ul>
12
12
  <li>\·$·{· value }</li>
13
13
  <li>\·$·{· value }</li>
@@ -17,9 +17,10 @@
17
17
  <li>234</li>
18
18
  </ul>
19
19
  <ul>
20
- <li>\·$·{· value }</li>
21
- <li>\·$·{· value }</li>
20
+ <li>123</li>
21
+ <li>234</li>
22
22
  </ul>
23
+ <strong>hello world!</strong>
23
24
  <script type="text/javascript">
24
25
  var a_0 =123;
25
26
  var a_1 =234;
package/hook.js CHANGED
@@ -2,30 +2,39 @@ const {
2
2
  types
3
3
  } = require("util");
4
4
 
5
+ /**
6
+ * 钩子管理类
7
+ * @class
8
+ */
5
9
  class Hook {
10
+ /**
11
+ * 创建钩子实例
12
+ */
6
13
  constructor() {
7
- this.dict_func = {};
8
- this.dict_action = {};
9
- this.dict_filter = {};
14
+ this._func = {};
15
+ this._action = {};
16
+ this._filter = {};
10
17
  }
11
18
  }
12
19
 
13
20
  /**
14
- * 排序
15
- * @param {Array} list 钩子列表
21
+ * 排序钩子列表
22
+ * @param {Array} list - 钩子列表
23
+ * @returns {Array} 排序后的列表
16
24
  */
17
- Hook.prototype.sort = function(list) {
25
+ Hook.prototype.sort = function (list) {
18
26
  return list.sort((o1, o2) => {
19
27
  return o2.sort - o1.sort
20
28
  });
21
29
  }
22
30
 
23
31
  /**
24
- * 获取别名
25
- * @param {Function} func 钩子函数
26
- * @param {String} alias 别名
32
+ * 获取函数别名
33
+ * @param {Function} func - 钩子函数
34
+ * @param {string} alias - 别名
35
+ * @returns {string} 函数别名
27
36
  */
28
- Hook.prototype.getName = function(func, alias) {
37
+ Hook.prototype._getName = function (func, alias) {
29
38
  if (!alias) {
30
39
  var str = func.toString();
31
40
  var name = str.between('function ', "(").trim();
@@ -40,70 +49,73 @@ Hook.prototype.getName = function(func, alias) {
40
49
 
41
50
  /**
42
51
  * 添加函数钩子
43
- * @param {String} name 钩子名称
44
- * @param {Function} func 钩子函数
45
- * @param {Number} sort 执行顺序
46
- * @param {String} alias 函数别名,用于删除
52
+ * @param {string} name - 钩子名称
53
+ * @param {Function} func - 钩子函数
54
+ * @param {number} sort - 执行顺序
55
+ * @param {string} alias - 函数别名
56
+ * @returns {string} 函数别名
47
57
  */
48
- Hook.prototype.addFunc = function(name, func, sort, alias) {
49
- alias = this.getName(func, alias);
58
+ Hook.prototype.addFunc = function (name, func, sort, alias) {
59
+ alias = this._getName(func, alias);
50
60
  var obj = {
51
61
  func,
52
62
  sort,
53
63
  alias
54
64
  }
55
- var arr = this.dict_func[name];
65
+ var arr = this._func[name];
56
66
  if (!arr) {
57
67
  arr = [];
58
68
  }
59
69
  arr.push(obj);
60
- this.dict_func[name] = this.sort(arr);
70
+ this._func[name] = this.sort(arr);
61
71
  return alias;
62
72
  }
63
73
 
64
74
  /**
65
75
  * 添加动作钩子
66
- * @param {String} name 钩子名称
67
- * @param {Function} func 钩子函数
68
- * @param {Number} sort 执行顺序
69
- * @param {String} alias 函数别名,用于删除
76
+ * @param {string} name - 钩子名称
77
+ * @param {Function} func - 钩子函数
78
+ * @param {number} sort - 执行顺序
79
+ * @param {string} alias - 函数别名
80
+ * @returns {string} 函数别名
70
81
  */
71
- Hook.prototype.addAction = function(name, func, sort, alias) {
72
- alias = this.getName(func, alias);
82
+ Hook.prototype.addAction = function (name, func, sort, alias) {
83
+ alias = this._getName(func, alias);
73
84
  var obj = {
74
85
  func,
75
86
  sort,
76
87
  alias
77
88
  }
78
- var arr = this.dict_action[name];
89
+ var arr = this._action[name];
79
90
  if (!arr) {
80
91
  arr = [];
81
92
  }
82
93
  arr.push(obj);
83
- this.dict_action[name] = this.sort(arr);
94
+ this._action[name] = this.sort(arr);
84
95
  return alias;
85
96
  }
86
97
 
87
98
  /**
88
99
  * 添加过滤钩子
89
- * @param {String} name 钩子名称
90
- * @param {Function} func 钩子函数
91
- * @param {Number} sort 执行顺序
92
- * @param {String} alias 函数别名,用于删除
100
+ * @param {string} name - 钩子名称
101
+ * @param {Function} func - 钩子函数
102
+ * @param {number} sort - 执行顺序
103
+ * @param {string} alias - 函数别名
104
+ * @returns {string} 函数别名
93
105
  */
94
- Hook.prototype.addFilter = function(name, func, sort, alias) {
95
- alias = this.getName(func, alias);
106
+ Hook.prototype.addFilter = function (name, func, sort, alias) {
107
+ alias = this._getName(func, alias);
96
108
  var obj = {
97
109
  func,
98
110
  sort,
99
111
  alias
100
112
  }
101
- var arr = this.dict_filter[name];
113
+ var arr = this._filter[name];
102
114
  if (!arr) {
103
115
  arr = [];
104
116
  }
105
117
  arr.push(obj);
106
- this.dict_filter[name] = this.sort(arr);
118
+ this._filter[name] = this.sort(arr);
107
119
  return alias;
108
120
  }
109
121
 
@@ -112,12 +124,17 @@ Hook.prototype.addFilter = function(name, func, sort, alias) {
112
124
  * @param {String} name 钩子名称
113
125
  * @param {String} alias 函数别名
114
126
  */
115
- Hook.prototype.delFunc = function(name, alias) {
116
- var arr = this.dict_func[name];
127
+ Hook.prototype.delFunc = function (name, alias) {
128
+ var arr = this._func[name];
117
129
  if (arr) {
118
- arr.del({
119
- alias
120
- });
130
+ var list = [];
131
+ for (var i = 0; i < arr.length; i++) {
132
+ var item = arr[i];
133
+ if (item.alias !== alias) {
134
+ list.push(item);
135
+ }
136
+ }
137
+ this._func[name] = list;
121
138
  }
122
139
  }
123
140
 
@@ -126,12 +143,17 @@ Hook.prototype.delFunc = function(name, alias) {
126
143
  * @param {String} name 钩子名称
127
144
  * @param {String} alias 函数别名
128
145
  */
129
- Hook.prototype.delAction = function(name, alias) {
130
- var arr = this.dict_action[name];
146
+ Hook.prototype.delAction = function (name, alias) {
147
+ var arr = this._action[name];
131
148
  if (arr) {
132
- arr.del({
133
- alias
134
- });
149
+ var list = [];
150
+ for (var i = 0; i < arr.length; i++) {
151
+ var item = arr[i];
152
+ if (item.alias !== alias) {
153
+ list.push(item);
154
+ }
155
+ }
156
+ this._action[name] = list;
135
157
  }
136
158
  }
137
159
 
@@ -140,32 +162,40 @@ Hook.prototype.delAction = function(name, alias) {
140
162
  * @param {String} name 钩子名称
141
163
  * @param {String} alias 函数别名
142
164
  */
143
- Hook.prototype.delFilter = function(name, alias = "") {
144
- var arr = this.dict_filter[name];
165
+ Hook.prototype.delFilter = function (name, alias = "") {
166
+ var arr = this._filter[name];
145
167
  if (arr) {
146
- arr.del({
147
- alias
148
- });
168
+ var list = [];
169
+ for (var i = 0; i < arr.length; i++) {
170
+ var item = arr[i];
171
+ if (item.alias !== alias) {
172
+ list.push(item);
173
+ }
174
+ }
175
+ this._filter[name] = list;
149
176
  }
150
177
  }
151
178
 
152
179
  /**
153
- * 运行动作钩子
154
- * @param {String} name 钩子名称
155
- * @param {Object} model 模型
156
- * @param {Object} args 附加参数
180
+ * 执行函数钩子
181
+ * @param {string} name - 钩子名称
182
+ * @param {Object} param - 参数
183
+ * @param {Function} func - 钩子函数
157
184
  */
158
- Hook.prototype.runAction = function(name, model, ...args) {
159
- var ret = "";
160
- var dict = this.dict_action;
161
- var list = dict[name];
162
- if (list) {
163
- for (var i = 0; i < list.length; i++) {
164
- var o = list[i];
185
+ Hook.prototype.runFunc = async function (name, model, req, ...args) {
186
+ var ret;
187
+ var list = this._func[name];
188
+ if (!list) {
189
+ var len = list.length;
190
+ for (var i = 0; i < len; i++) {
191
+ var item = list[i];
165
192
  try {
166
- ret += o.func(model, ...args);
193
+ ret = item.func(model, req, ...args);
194
+ if (ret) {
195
+ ret = await ret;
196
+ }
167
197
  } catch (err) {
168
- $.log.error(ret);
198
+ $.log.error(`[${this.constructor.name}] 执行函数钩子失败 ${item.alias}`, err);
169
199
  }
170
200
  }
171
201
  }
@@ -173,22 +203,22 @@ Hook.prototype.runAction = function(name, model, ...args) {
173
203
  }
174
204
 
175
205
  /**
176
- * 运行过滤钩子
177
- * @param {String} name 钩子名称
178
- * @param {Object} model 模型
179
- * @param {Object} ret 结果
180
- * @param {Object} args 附加参数
206
+ * 执行动作钩子
207
+ * @param {string} name - 钩子名称
208
+ * @param {Object} model - 模型对象
209
+ * @param {...any} args - 附加参数
210
+ * @returns {Object} 执行结果
181
211
  */
182
- Hook.prototype.runFilter = function(name, model, ret, ...args) {
183
- var dict = this.dict_filter;
184
- var list = dict[name];
212
+ Hook.prototype.runAction = function (name, model, ...args) {
213
+ var ret = '';
214
+ var list = this._action[name];
185
215
  if (list) {
186
216
  for (var i = 0; i < list.length; i++) {
187
217
  var o = list[i];
188
218
  try {
189
- ret = o.func(model, ret, ...args);
219
+ ret += o.func(model, ...args);
190
220
  } catch (err) {
191
- $.log.error(ret);
221
+ $.log.error(`[${this.constructor.name}] 执行动作钩子失败 ${o.alias}`, err);
192
222
  }
193
223
  }
194
224
  }
@@ -196,30 +226,84 @@ Hook.prototype.runFilter = function(name, model, ret, ...args) {
196
226
  }
197
227
 
198
228
  /**
199
- * 运行函数钩子
200
- * @param {String} name 钩子名称
201
- * @param {Object} model 模型
202
- * @param {Object} req 请求参数
203
- * @param {Object} args 附加参数
229
+ * 执行过滤钩子
230
+ * @param {string} name - 钩子名称
231
+ * @param {Object} model - 模型对象
232
+ * @param {Object} ret - 返回值
233
+ * @param {...any} args - 附加参数
234
+ * @returns {Object} 过滤后的参数
204
235
  */
205
- Hook.prototype.runFunc = async function(name, model, req, ...args) {
206
- var ret;
207
- var dict = this.dict_func;
208
- var list = dict[name];
209
- if (list) {
210
- for (var i = 0; i < list.length; i++) {
211
- var o = list[i];
236
+ Hook.prototype.runFilter = function (name, model, ret, ...args) {
237
+ var list = this._filter[name];
238
+ if (!list) {
239
+ var len = list.length;
240
+ for (var i = 0; i < len; i++) {
241
+ var item = list[i];
212
242
  try {
213
- ret = o.func(model, req, ...args);
214
- if (ret) {
215
- ret = await ret;
216
- }
243
+ ret = item.func(model, ret, ...args) || ret;
217
244
  } catch (err) {
218
- $.log.error(ret);
245
+ $.log.error(`[${this.constructor.name}] 执行过滤钩子失败 ${item.alias}`, err);
219
246
  }
220
247
  }
221
248
  }
222
249
  return ret;
223
250
  }
224
251
 
252
+ /**
253
+ * 删除钩子
254
+ * @param {string} name - 钩子名称
255
+ * @param {string} alias - 函数别名
256
+ */
257
+ Hook.prototype.del = function (name, alias) {
258
+ var arr = this._func[name];
259
+ if (arr) {
260
+ var list = [];
261
+ for (var i = 0; i < arr.length; i++) {
262
+ var item = arr[i];
263
+ if (item.alias !== alias) {
264
+ list.push(item);
265
+ }
266
+ }
267
+ this._func[name] = list;
268
+ }
269
+ arr = this._action[name];
270
+ if (arr) {
271
+ var list = [];
272
+ for (var i = 0; i < arr.length; i++) {
273
+ var item = arr[i];
274
+ if (item.alias !== alias) {
275
+ list.push(item);
276
+ }
277
+ }
278
+ this._action[name] = list;
279
+ }
280
+ arr = this._filter[name];
281
+ if (arr) {
282
+ var list = [];
283
+ for (var i = 0; i < arr.length; i++) {
284
+ var item = arr[i];
285
+ if (item.alias !== alias) {
286
+ list.push(item);
287
+ }
288
+ }
289
+ this._filter[name] = list;
290
+ }
291
+ }
292
+
293
+ /**
294
+ * 清空钩子
295
+ * @param {string} name - 钩子名称
296
+ */
297
+ Hook.prototype.clear = function (name) {
298
+ if (name) {
299
+ delete this._func[name];
300
+ delete this._action[name];
301
+ delete this._filter[name];
302
+ } else {
303
+ this._func = {};
304
+ this._action = {};
305
+ this._filter = {};
306
+ }
307
+ }
308
+
225
309
  module.exports = Hook;
package/index.js CHANGED
@@ -1,6 +1,8 @@
1
- require('mm_logs');
2
1
  const template = require('art-template');
3
2
  const Hook = require('./hook.js');
3
+ const {
4
+ Base
5
+ } = require('mm_expand');
4
6
  const {
5
7
  nativeRule,
6
8
  artRule,
@@ -22,18 +24,13 @@ if (!$.hook) {
22
24
  * 模板类
23
25
  * @class
24
26
  */
25
- class Tpl {
27
+ class Tpl extends Base {
26
28
  /**
27
- * 创建模板帮助类函数 (构造函数)
28
- * @param {String} scope 作用域
29
- * @param {String} dir 文件存储路径
30
- * @constructor
29
+ * 创建模板实例
30
+ * @param {Object} config - 配置选项
31
31
  */
32
32
  constructor(config) {
33
- /**
34
- * 配置参数
35
- */
36
- this.config = {
33
+ super(Object.assign({
37
34
  root: $.runPath + '',
38
35
  default_dir: "./template/",
39
36
  default_theme: 'default',
@@ -42,7 +39,7 @@ class Tpl {
42
39
  cache_root: './cache'.fullname(),
43
40
  cache_extname: '.cache.html',
44
41
  change_str: ["\\#", "\\<", "\\${", "\\/*", '\\"""'],
45
- };
42
+ }, config));
46
43
 
47
44
  /**
48
45
  * 模板引擎
@@ -50,13 +47,10 @@ class Tpl {
50
47
  this.template = template;
51
48
 
52
49
  /**
53
- * 视图背包
50
+ * 视图数据
54
51
  */
55
52
  this.viewBag = {};
56
53
 
57
- // 修改配置
58
- this.setConfig(config);
59
-
60
54
  /**
61
55
  * 模板根目录
62
56
  */
@@ -86,10 +80,10 @@ class Tpl {
86
80
  }
87
81
 
88
82
  /**
89
- * 修改配置
90
- * @param {Object} config 配置参数
83
+ * 设置配置
84
+ * @param {Object} config - 配置参数
91
85
  */
92
- Tpl.prototype.setConfig = function(config) {
86
+ Tpl.prototype.setConfig = function (config) {
93
87
  if (config) {
94
88
  this.config = Object.assign(this.config, config);
95
89
  }
@@ -104,35 +98,44 @@ Tpl.prototype.setConfig = function(config) {
104
98
 
105
99
 
106
100
  /**
107
- * 执行函数钩子
108
- * @param {String} name 名称
109
- * @param {Object} req 请求参数
110
- * @param {Object} args 附加参数
101
+ * 执行钩子函数
102
+ * @param {string} name - 钩子名称
103
+ * @param {Object} req - 请求参数
104
+ * @param {...any} args - 附加参数
105
+ * @returns {any} 执行结果
111
106
  */
112
- Tpl.prototype.runFunc = function(name, req, ...args) {
107
+ Tpl.prototype.runFunc = function (name, req, ...args) {
113
108
  return $.hook.runFunc(name, this.viewBag, req, ...args);
114
109
  }
115
110
 
111
+ /**
112
+ * 执行钩子函数
113
+ * @param {string} name - 钩子名称
114
+ * @param {Object} req - 请求参数
115
+ * @param {...any} args - 附加参数
116
+ * @returns {any} 执行结果
117
+ */
118
+ Tpl.prototype.runHook = Tpl.prototype.runFunc;
116
119
 
117
120
  /**
118
- * 初始化之前
119
- * @param {Object} config 配置参数
121
+ * 初始化前处理
122
+ * @param {Object} config - 配置参数
120
123
  */
121
- Tpl.prototype.init_before = function(config) {}
124
+ Tpl.prototype._initBefore = function (config) { }
122
125
 
123
126
  /**
124
- * 初始化主体
125
- * @param {Object} config 配置参数
127
+ * 初始化主处理
128
+ * @param {Object} config - 配置参数
126
129
  */
127
- Tpl.prototype.init_main = function(config) {
130
+ Tpl.prototype._initMain = function (config) {
128
131
  this.setConfig(config);
129
132
  }
130
133
 
131
134
  /**
132
- * 初始化之后
133
- * @param {Object} config 配置参数
135
+ * 初始化后处理
136
+ * @param {Object} config - 配置参数
134
137
  */
135
- Tpl.prototype.init_after = function(config) {
138
+ Tpl.prototype._initAfter = function (config) {
136
139
  var lt = this.config.change_str;
137
140
  for (var i = 0; i < lt.length; i++) {
138
141
  var str = lt[i];
@@ -141,30 +144,30 @@ Tpl.prototype.init_after = function(config) {
141
144
  this.regex_in.push(new RegExp(rx_in, "gi"));
142
145
  var str_in = str.split("").join("·") + "·";
143
146
  this.str_in.push(str_in);
144
- var rx_out = str_in.replace("\\", "\\\\").replace("$", "\\$").replace("[", "\\[").replace("]", "\\]")
145
- .replace(
146
- "{", "\\{").replace("}", "\\}").replace("/", "\\/").replace("*", "\\*");
147
+ var rx_out = str_in.replace("\\", "\\\\").replace("$", "\\$").replace("[", "\\[").replace("]", "\\]").replace(
148
+ "{", "\\{").replace("}", "\\}").replace("/", "\\/").replace("*", "\\*");
147
149
  this.regex_out.push(new RegExp(rx_out, "gi"));
148
150
  this.str_out.push(str.replace("\\", ""));
149
151
  }
150
152
  }
151
153
 
152
154
  /**
153
- * 初始化
154
- * @param {Object} config 配置参数
155
+ * 初始化模板
156
+ * @param {Object} config - 配置参数
155
157
  */
156
- Tpl.prototype.init = function(config) {
157
- this.init_before(config);
158
- this.init_main(config);
159
- this.init_after(config);
158
+ Tpl.prototype.init = function (config) {
159
+ this._initBefore(config);
160
+ this._initMain(config);
161
+ this._initAfter(config);
160
162
  }
161
163
 
162
164
  /**
163
- * 转译字符
164
- * @param {String} body 字符串
165
- * @param {Boolean} restore 是否还原
165
+ * 转义字符
166
+ * @param {string} body - 字符串
167
+ * @param {boolean} restore - 是否还原
168
+ * @returns {string} 转义后的字符串
166
169
  */
167
- Tpl.prototype.change = function(body, restore) {
170
+ Tpl.prototype.escape = function (body, restore) {
168
171
  if (!body) {
169
172
  return body
170
173
  }
@@ -181,18 +184,18 @@ Tpl.prototype.change = function(body, restore) {
181
184
  }
182
185
 
183
186
  /**
184
- * 渲染模板(之前)
185
- * @param {String} body 模板
186
- * @param {Object} model 数据模型
187
- * @param {Object} options 配置参数
188
- * @return {String} 渲染结果
187
+ * 渲染前处理
188
+ * @param {string} body - 模板内容
189
+ * @param {Object} model - 数据模型
190
+ * @param {Object} options - 配置参数
191
+ * @returns {string} 渲染结果
189
192
  */
190
- Tpl.prototype.render_before = function(body, model, options) {
193
+ Tpl.prototype._renderBefore = function (body, model, options) {
191
194
  var ret = "";
192
195
  if (options && options.cache) {
193
196
  var f = options.cache_filename;
194
197
  if (f) {
195
- if (f.replace('./', '').indexOf('.') == -1 && !f.endWith(this.config.cache_extname)) {
198
+ if (f.replace('./', '').indexOf('.') == -1 && !f.endsWith(this.config.cache_extname)) {
196
199
  f += this.config.cache_extname;
197
200
  }
198
201
  var file = f.fullname(this.config.cache_root);
@@ -206,10 +209,11 @@ Tpl.prototype.render_before = function(body, model, options) {
206
209
  };
207
210
 
208
211
  /**
209
- * 用于注入全局函数
210
- * @param {Object} m
212
+ * 注入全局函数前处理
213
+ * @param {Object} m - 模型对象
214
+ * @returns {Object} 处理后的模型
211
215
  */
212
- Tpl.prototype.loadFunc_before = function(m) {
216
+ Tpl.prototype._loadFuncBefore = function (m) {
213
217
  // m.$i18n = function(key) {
214
218
  // return $.i18n(key);
215
219
  // }
@@ -217,38 +221,44 @@ Tpl.prototype.loadFunc_before = function(m) {
217
221
  }
218
222
 
219
223
  /**
220
- * 加载函数
221
- * @param {Object} m 模型
224
+ * 加载函数到模型
225
+ * @param {Object} m - 模型对象
226
+ * @returns {Object} 处理后的模型
222
227
  */
223
- Tpl.prototype.loadFunc = function(m) {
224
- this.loadFunc_before(m);
228
+ Tpl.prototype.loadFunc = function (m) {
229
+ this._loadFuncBefore(m);
225
230
  var _this = this;
226
- m.view = function(file, model, options) {
231
+ m.view = function (file, model, options) {
227
232
  return _this.view(file, Object.assign({}, m, model), options);
228
233
  }
229
- m.render = function(body, model, options) {
234
+ m.render = function (body, model, options) {
230
235
  return _this.render(body, Object.assign({}, m, model), options);
231
236
  }
232
- m.hook_action = function(name, ...args) {
237
+ m.hookAction = function (name, ...args) {
233
238
  return $.hook.runAction(name, m, ...args);
234
239
  }
235
- m.hook_filter = function(name, content, ...args) {
240
+ m.hookFilter = function (name, content, ...args) {
236
241
  if (!content) {
237
242
  content = "";
238
243
  }
239
244
  return $.hook.runFilter(name, m, content, ...args);
240
245
  }
246
+ if (global.$ && $.lang) {
247
+ m.$t = function (key, ...args) {
248
+ return $.lang.get(key, ...args);
249
+ }
250
+ }
241
251
  return m;
242
252
  }
243
253
 
244
254
  /**
245
- * 渲染模板(主要)
246
- * @param {String} body 模板
247
- * @param {Object} model 数据模型
248
- * @param {Object} options 配置参数
249
- * @return {String} 渲染结果
255
+ * 渲染主处理
256
+ * @param {string} body - 模板内容
257
+ * @param {Object} model - 数据模型
258
+ * @param {Object} options - 配置参数
259
+ * @returns {string} 渲染结果
250
260
  */
251
- Tpl.prototype.render_main = function(body, model, options) {
261
+ Tpl.prototype._renderMain = function (body, model, options) {
252
262
  var ret = "";
253
263
  if (body) {
254
264
  var m = Object.assign({}, {
@@ -262,18 +272,18 @@ Tpl.prototype.render_main = function(body, model, options) {
262
272
  };
263
273
 
264
274
  /**
265
- * 渲染模板(之后)
266
- * @param {String} body 模板
267
- * @param {Object} model 数据模型
268
- * @param {Object} options 配置参数
269
- * @return {String} 渲染结果
275
+ * 渲染后处理
276
+ * @param {string} body - 模板内容
277
+ * @param {Object} model - 数据模型
278
+ * @param {Object} options - 配置参数
279
+ * @returns {string} 渲染结果
270
280
  */
271
- Tpl.prototype.render_after = function(body, model, options) {
281
+ Tpl.prototype._renderAfter = function (body, model, options) {
272
282
  if (body) {
273
283
  if (options) {
274
284
  var f = options.cache_filename;
275
285
  if (f) {
276
- if (f.replace('./', '').indexOf('.') == -1 && !f.endWith(this.config.cache_extname)) {
286
+ if (f.replace('./', '').indexOf('.') == -1 && !f.endsWith(this.config.cache_extname)) {
277
287
  f += this.config.cache_extname;
278
288
  }
279
289
  var file = f.fullname(this.config.cache_root);
@@ -285,18 +295,18 @@ Tpl.prototype.render_after = function(body, model, options) {
285
295
  };
286
296
 
287
297
  /**
288
- * 渲染模板
289
- * @param {String} body 模板
290
- * @param {Object} model 数据模型
291
- * @param {Object} options 配置参数
292
- * @return {String} view 渲染后的视图
298
+ * 渲染子模板
299
+ * @param {string} body - 模板内容
300
+ * @param {Object} model - 数据模型
301
+ * @param {Object} options - 配置参数
302
+ * @returns {string} 渲染结果
293
303
  */
294
- Tpl.prototype.renderSub = function(body, model, options) {
295
- var ret = this.render_before(body, model, options);
304
+ Tpl.prototype._renderSub = function (body, model, options) {
305
+ var ret = this._renderBefore(body, model, options);
296
306
  if (!ret) {
297
- ret = this.render_main(body, model, options);
307
+ ret = this._renderMain(body, model, options);
298
308
  if (ret) {
299
- ret = this.render_after(ret, model, options);
309
+ ret = this._renderAfter(ret, model, options);
300
310
  }
301
311
  }
302
312
  return ret;
@@ -304,36 +314,36 @@ Tpl.prototype.renderSub = function(body, model, options) {
304
314
 
305
315
  /**
306
316
  * 渲染模板
307
- * @param {String} body 模板
308
- * @param {Object} model 数据模型
309
- * @param {Object} options 配置参数
310
- * @return {String} view 渲染后的视图
317
+ * @param {string} body - 模板内容
318
+ * @param {Object} model - 数据模型
319
+ * @param {Object} options - 配置参数
320
+ * @returns {string} 渲染结果
311
321
  */
312
- Tpl.prototype.render = function(body, model, options) {
313
- body = this.change(body);
314
- body = this.renderSub(body, model, options);
315
- return this.change(body, true);
322
+ Tpl.prototype.render = function (body, model, options) {
323
+ body = this.escape(body);
324
+ body = this._renderSub(body, model, options);
325
+ return this.escape(body, true);
316
326
  };
317
327
 
318
328
  /**
319
- * 编译模板并返回渲染函数
320
- * @param {String} body 模板
321
- * @param {Object} options 配置参数
322
- * @return {Function} 渲染函数
329
+ * 编译模板
330
+ * @param {string} body - 模板内容
331
+ * @param {Object} options - 配置参数
332
+ * @returns {Function} 渲染函数
323
333
  */
324
- Tpl.prototype.compile = function(body, options) {
334
+ Tpl.prototype.compile = function (body, options) {
325
335
  return template.compile(body, options);
326
336
  };
327
337
 
328
338
  /**
329
339
  * 渲染模板文件
330
- * @param {String} file 模板文件路径
331
- * @param {Object} model 数据模型
332
- * @param {Object} options 配置参数
333
- * @return {String} 渲染后的视图
340
+ * @param {string} file - 模板文件路径
341
+ * @param {Object} model - 数据模型
342
+ * @param {Object} options - 配置参数
343
+ * @returns {string} 渲染结果
334
344
  */
335
- Tpl.prototype.view = function(file, model, options) {
336
- if (file.replace('./', '').indexOf('.') === -1 && !file.endWith(this.config.extname)) {
345
+ Tpl.prototype.view = function (file, model, options) {
346
+ if (file.replace('./', '').indexOf('.') === -1 && !file.endsWith(this.config.extname)) {
337
347
  file += this.config.extname;
338
348
  }
339
349
  var f = file.fullname(this.dir + 'diy');
@@ -361,9 +371,9 @@ Tpl.prototype.view = function(file, model, options) {
361
371
  }
362
372
  var body = f.loadText();
363
373
  if (body) {
364
- body = this.change(body);
365
- body = this.renderSub(body, model, options);
366
- body = this.change(body, true);
374
+ body = this.escape(body);
375
+ body = this._renderSub(body, model, options);
376
+ body = this.escape(body, true);
367
377
  return body;
368
378
  } else {
369
379
  return "";
@@ -371,42 +381,42 @@ Tpl.prototype.view = function(file, model, options) {
371
381
  };
372
382
 
373
383
  /**
374
- * 设置视图背包成员
375
- * @param {Object} obj 设置的对象
384
+ * 设置视图数据
385
+ * @param {Object} obj - 要设置的数据对象
376
386
  */
377
- Tpl.prototype.set = function(obj) {
387
+ Tpl.prototype.set = function (obj) {
378
388
  $.push(this.viewBag, obj, true);
379
389
  };
380
390
 
381
391
  /**
382
- * 删除视图背包成员
383
- * @param {Array|String} arrOrStr 删除的键或键数组
392
+ * 删除视图数据
393
+ * @param {Array|string} keys - 要删除的键或键数组
384
394
  */
385
- Tpl.prototype.del = function(arrOrStr) {
386
- if (typeof(arrOrStr) == 'string') {
387
- delete this.viewBag[arrOrStr];
395
+ Tpl.prototype.del = function (keys) {
396
+ if (typeof (keys) == 'string') {
397
+ delete this.viewBag[keys];
388
398
  } else {
389
- arr.map(function(k) {
399
+ keys.map(function (k) {
390
400
  delete this.viewBag[k];
391
401
  });
392
402
  }
393
403
  };
394
404
 
395
405
  /**
396
- * 获取视图背包成员
397
- * @param {String} key 获取的对象键名
398
- * @return {Object} 值
406
+ * 获取视图数据
407
+ * @param {string} key - 数据键名
408
+ * @returns {any} 数据值
399
409
  */
400
- Tpl.prototype.get = function(key) {
410
+ Tpl.prototype.get = function (key) {
401
411
  return this.viewBag[key];
402
412
  };
403
413
 
404
414
  /**
405
415
  * 清除缓存
406
- * @param {Object} path 检索的路径
407
- * @param {Object} file 文件名
416
+ * @param {string} path - 缓存路径
417
+ * @param {string} file - 文件名
408
418
  */
409
- Tpl.prototype.clear_cache = function(path, file) {
419
+ Tpl.prototype.clearCache = function (path, file) {
410
420
  var cg = this.config;
411
421
  var root = cg.cache_root;
412
422
  var dir = path ? path.fullname(root) : root;
@@ -421,9 +431,11 @@ Tpl.prototype.clear_cache = function(path, file) {
421
431
  });
422
432
  };
423
433
 
424
- if (global.$) {
434
+ if (global.$ && !global.$.Tpl) {
425
435
  $.Tpl = Tpl;
426
436
  $.tpl = new Tpl();
427
437
  }
428
438
 
429
- module.exports = Tpl;
439
+ module.exports = {
440
+ Tpl
441
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_tpl",
3
- "version": "2.4.4",
3
+ "version": "2.4.5",
4
4
  "description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -28,6 +28,6 @@
28
28
  "homepage": "https://gitee.com/qiuwenwu91/mm_tpl#readme",
29
29
  "dependencies": {
30
30
  "art-template": "^4.13.4",
31
- "mm_logs": "^1.5.3"
31
+ "mm_expand": "^1.9.7"
32
32
  }
33
33
  }
@@ -6,8 +6,8 @@
6
6
  </head>
7
7
  <body>
8
8
  <h5>${viewBag.name}</h5>
9
- <p>${@hook_filter('test', '你真的是人才!')}</p>
10
- <p><!--{@hook_filter('test', '你真的是人才!', {key: '123'})}--></p>
9
+ <p>${@hookFilter('test', '你真的是人才!')}</p>
10
+ <p><!--{@hookFilter('test', '你真的是人才!', {key: '123'})}--></p>
11
11
  <ul>
12
12
  ${loop arr value idx}
13
13
  <li>\${ value }</li>
@@ -20,9 +20,10 @@
20
20
  </ul>
21
21
  <ul>
22
22
  <!--{loop arr value idx}-->
23
- <li>\${ value }</li>
23
+ <li>${ value }</li>
24
24
  <!--{/loop}-->
25
25
  </ul>
26
+ <strong>${message}</strong>
26
27
  <script type="text/javascript">
27
28
  /*[loop arr value idx]*/
28
29
  var a_${idx} = ${value};
@@ -1,5 +1,5 @@
1
1
  <div class="bar_user">
2
- ${@hook_filter('test', hook_action('test', '好的'), '真的')}
2
+ ${@hookFilter('test', hook_action('test', '好的'), '真的')}
3
3
  <div>用户ID:${viewBag.user_id}</div>
4
4
  <div>当前项目ID:${viewBag.project_id}</div>
5
5
  <div>${message}</div>
package/test.js CHANGED
@@ -1,4 +1,4 @@
1
- const Tpl = require('./index.js');
1
+ const {Tpl} = require('./index.js');
2
2
 
3
3
  /**
4
4
  * 基础模板渲染测试函数
@@ -75,6 +75,7 @@ async function test() {
75
75
 
76
76
  // 添加列表函数钩子
77
77
  $.hook.addFunc('list', async function(m, req, type) {
78
+ console.log("参数", m, req, type);
78
79
  var query = req.query;
79
80
  if (query.user_id) {
80
81
  m.user_id = query.user_id;
@@ -125,7 +126,7 @@ async function test() {
125
126
  });
126
127
  // 删除指定目录下的缓存文件
127
128
  // tpl.clear_cache('./mall');
128
- $.log.debug(body);
129
+ $.log.debug("渲染结果", body);
129
130
  }
130
131
 
131
132
  // 执行测试
package/tpl_rule.js CHANGED
@@ -2,9 +2,19 @@
2
2
 
3
3
  /**
4
4
  * 原生模板语法规则
5
+ * @type {Object}
5
6
  */
6
7
  var nativeRule = {
7
8
  test: /[ \t\r\n]*<!--\[(#?)((?:==|=#|[=-])?)[ \t]*([\w\W]*?)[ \t]*(-?)\]-->/,
9
+
10
+ /**
11
+ * 处理原生模板语法
12
+ * @param {string} match - 匹配的字符串
13
+ * @param {string} comment - 注释标记
14
+ * @param {string} output - 输出类型
15
+ * @param {string} code - 代码内容
16
+ * @returns {Object} 处理结果
17
+ */
8
18
  use: function use(match, comment, output, code /*, trimMode*/ ) {
9
19
  output = {
10
20
  '-': 'raw',
@@ -33,9 +43,19 @@ var nativeRule = {
33
43
 
34
44
  /**
35
45
  * 简洁模板语法规则
46
+ * @type {Object}
36
47
  */
37
48
  var artRule = {
38
49
  test: /[ \t\r\n]*\$\{([@#]?)[ \t]*(\/?)([\w\W]*?)[ \t]*\}/,
50
+
51
+ /**
52
+ * 处理简洁模板语法
53
+ * @param {string} match - 匹配的字符串
54
+ * @param {string} raw - 原始输出标记
55
+ * @param {string} close - 闭合标记
56
+ * @param {string} code - 代码内容
57
+ * @returns {Object} 处理结果
58
+ */
39
59
  use: function use(match, raw, close, code) {
40
60
  var compiler = this;
41
61
  var options = compiler.options;
@@ -179,9 +199,11 @@ var artRule = {
179
199
  return result;
180
200
  },
181
201
 
182
- // 将多个 javascript 表达式拆分成组
183
- // 支持基本运算、三元表达式、取值、运行函数,不支持 `typeof value` 操作
184
- // 只支持 string、number、boolean、null、undefined 这几种类型声明,不支持 function、object、array
202
+ /**
203
+ * 将多个javascript表达式拆分成组
204
+ * @param {Array} esTokens - ES令牌数组
205
+ * @returns {Array} 分组后的表达式数组
206
+ */
185
207
  _split: function _split(esTokens) {
186
208
  esTokens = esTokens.filter(function(_ref) {
187
209
  var type = _ref.type;
@@ -220,28 +242,32 @@ var artRule = {
220
242
  };
221
243
 
222
244
  /**
223
- * html注释模板语法规则
245
+ * HTML注释模板语法规则
246
+ * @type {Object}
224
247
  */
225
248
  var htmlRule = Object.assign({}, artRule, {
226
249
  test: /[ \t\r\n]*<!--\{([@#]?)[ \t]*(\/?)([\w\W]*?)[ \t]*\}-->/
227
250
  });
228
251
 
229
252
  /**
230
- * js注释模板语法规则
253
+ * JS注释模板语法规则
254
+ * @type {Object}
231
255
  */
232
256
  var jsRule = Object.assign({}, artRule, {
233
257
  test: /[ \t\r\n]*\/\*\[([@#]?)[ \t]*(\/?)([\w\W]*?)[ \t]*\]\*\//
234
258
  });
235
259
 
236
260
  /**
237
- * python注释模板语法规则
261
+ * Python注释模板语法规则
262
+ * @type {Object}
238
263
  */
239
264
  var pyRule = Object.assign({}, artRule, {
240
265
  test: /[ \t\r\n]*##([@#]?)[ \t]*(\/?)([\w\W]*?)[ \t]*##/
241
266
  });
242
267
 
243
268
  /**
244
- * python注释模板语法规则
269
+ * Python三引号注释模板语法规则
270
+ * @type {Object}
245
271
  */
246
272
  var pyRule2 = Object.assign({}, artRule, {
247
273
  test: /"""#([@#]?)[ \t]*(\/?)([\w\W]*?)[ \t]*#"""/
@@ -255,3 +281,7 @@ module.exports = {
255
281
  pyRule,
256
282
  pyRule2
257
283
  };
284
+
285
+
286
+
287
+