mm_tpl 2.4.3 → 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/index.js CHANGED
@@ -1,6 +1,8 @@
1
- require('mm_expand');
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.set_config(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.set_config = 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.set_config = 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) {
128
- this.set_config(config);
130
+ Tpl.prototype._initMain = function (config) {
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.render_sub = 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.render_sub = 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.render_sub(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.render_sub(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.3",
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_expand": "^1.8.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
  * 基础模板渲染测试函数
@@ -10,7 +10,7 @@ async function test() {
10
10
  var body = tpl.render("<a>${test} + 123123</a>", {
11
11
  test: 123
12
12
  });
13
- console.log(body);
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
- console.log(body);
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
- console.log("测试开始")
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
- console.log(tpl.error);
57
- console.log(body);
56
+ $.log.debug(tpl.error);
57
+ $.log.debug(body);
58
58
  }
59
59
 
60
60
  /**
@@ -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;
@@ -82,7 +83,7 @@ async function test() {
82
83
  if (query.project_id) {
83
84
  m.project_id = query.project_id;
84
85
  }
85
- console.log("列表", m);
86
+ $.log.debug("列表", m);
86
87
  });
87
88
 
88
89
  // 添加动作钩子
@@ -96,7 +97,7 @@ async function test() {
96
97
 
97
98
  // 添加过滤器钩子
98
99
  $.hook.addFilter('test', function(m, ret, param) {
99
- console.log("测试", ret);
100
+ $.log.debug("测试", ret);
100
101
  return ret.replace("你", "您");
101
102
  }, 1, "测试过滤");
102
103
 
@@ -125,7 +126,7 @@ async function test() {
125
126
  });
126
127
  // 删除指定目录下的缓存文件
127
128
  // tpl.clear_cache('./mall');
128
- console.log(body);
129
+ $.log.debug("渲染结果", body);
129
130
  }
130
131
 
131
132
  // 执行测试