mm_tpl 2.1.3 → 2.1.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.
@@ -5,23 +5,13 @@
5
5
  <title>超级美眉Art模板引擎</title>
6
6
  </head>
7
7
  <body>
8
- <h5>系统名称</h5>
9
- <ul>
10
- <li>123</li>
11
- <li>234</li>
12
- </ul>
13
- <ul>
14
- <li>123</li>
15
- <li>234</li>
16
- </ul>
17
- <ul>
18
- <li>123</li>
19
- <li>234</li>
20
- </ul>
21
- <script type="text/javascript">
22
- var a_0 =123;
23
- var a_1 =234;
24
- </script>系统名称计算
25
- 123123'priceNIHAO'#计算
8
+ <div>游戏主题</div>
9
+ <div><div class="bar_user"><div>您好吗?</div><div>你好!</div></div>
10
+ <div class="bar">导航栏</div>
11
+ <style>
12
+ .bar {
13
+ font-weight: 800;
14
+ }
15
+ </style></div>
26
16
  </body>
27
17
  </html>
package/hook.js ADDED
@@ -0,0 +1,124 @@
1
+ class Hook {
2
+ constructor() {
3
+ this.dict_action = {};
4
+ this.dict_filter = {};
5
+ }
6
+ }
7
+
8
+ /**
9
+ * 排序
10
+ * @param {Array} list 钩子列表
11
+ */
12
+ Hook.prototype.sort = function(list) {
13
+ return list.sort((o1, o2) => {
14
+ return o2.sort - o1.sort
15
+ });
16
+ }
17
+
18
+ /**
19
+ * 添加动作钩子
20
+ * @param {String} name 钩子名称
21
+ * @param {Function} func 钩子函数
22
+ * @param {Number} sort 执行顺序
23
+ * @param {String} alias 函数别名,用于删除
24
+ */
25
+ Hook.prototype.addAction = function(name, func, sort, alias) {
26
+ var obj = {
27
+ func,
28
+ sort,
29
+ alias
30
+ }
31
+ var arr = this.dict_action[name];
32
+ if (!arr) {
33
+ arr = [];
34
+ }
35
+ arr.push(obj);
36
+ this.dict_action[name] = this.sort(arr);
37
+ }
38
+
39
+ /**
40
+ * 添加过滤钩子
41
+ * @param {String} name 钩子名称
42
+ * @param {Function} func 钩子函数
43
+ * @param {Number} sort 执行顺序
44
+ * @param {String} alias 函数别名,用于删除
45
+ */
46
+ Hook.prototype.addFilter = function(name, func, sort, alias) {
47
+ var obj = {
48
+ func,
49
+ sort,
50
+ alias
51
+ }
52
+ var arr = this.dict_filter[name];
53
+ if (!arr) {
54
+ arr = [];
55
+ }
56
+ arr.push(obj);
57
+ this.dict_filter[name] = this.sort(arr);
58
+ }
59
+
60
+ /**
61
+ * 删除动作钩子
62
+ * @param {String} name 钩子名称
63
+ * @param {String} alias 函数别名
64
+ */
65
+ Hook.prototype.delAction = function(name, alias) {
66
+ var arr = this.dict_action[name];
67
+ if (arr) {
68
+ arr.del({
69
+ alias
70
+ });
71
+ }
72
+ }
73
+
74
+ /**
75
+ * 删除过滤钩子
76
+ * @param {String} name 钩子名称
77
+ * @param {String} alias 函数别名
78
+ */
79
+ Hook.prototype.delFilter = function(name, alias = "") {
80
+ var arr = this.dict_filter[name];
81
+ if (arr) {
82
+ arr.del({
83
+ alias
84
+ });
85
+ }
86
+ }
87
+
88
+ /**
89
+ * 运行动作钩子
90
+ * @param {String} name 钩子名称
91
+ * @param {Object} ret 结果
92
+ * @param {Object} args 附加参数
93
+ */
94
+ Hook.prototype.runAction = function(name, ret, ...args) {
95
+ var dict = this.dict_action;
96
+ var list = dict[name];
97
+ if (list) {
98
+ for (var i = 0; i < list.length; i++) {
99
+ var o = list[i];
100
+ ret += o.func(ret, ...args);
101
+ }
102
+ }
103
+ return ret;
104
+ }
105
+
106
+ /**
107
+ * 运行过滤钩子
108
+ * @param {String} name 钩子名称
109
+ * @param {Object} ret 结果
110
+ * @param {Object} args 附加参数
111
+ */
112
+ Hook.prototype.runFilter = function(name, ret, ...args) {
113
+ var dict = this.dict_filter;
114
+ var list = dict[name];
115
+ if (list) {
116
+ for (var i = 0; i < list.length; i++) {
117
+ var o = list[i];
118
+ ret = o.func(ret, ...args);
119
+ }
120
+ }
121
+ return ret;
122
+ }
123
+
124
+ module.exports = Hook;
package/index.js CHANGED
@@ -1,5 +1,6 @@
1
- require('mm_expand');
1
+ require('mm_expand');
2
2
  const template = require('art-template');
3
+ const Hook = require('./hook.js');
3
4
  const {
4
5
  nativeRule,
5
6
  artRule,
@@ -13,6 +14,10 @@ if (!$.globalBag) {
13
14
  $.globalBag = {};
14
15
  }
15
16
 
17
+ if (!$.hook) {
18
+ $.hook = new Hook();
19
+ }
20
+
16
21
  /**
17
22
  * 模板类
18
23
  * @class
@@ -30,6 +35,8 @@ class Tpl {
30
35
  */
31
36
  this.config = {
32
37
  root: $.runPath + '',
38
+ defualt_dir: "./template/",
39
+ defualt_theme: 'defualt',
33
40
  extname: ".html",
34
41
  rules: [nativeRule, artRule, htmlRule, jsRule, pyRule, pyRule2],
35
42
  cache_root: './cache'.fullname(),
@@ -48,11 +55,21 @@ class Tpl {
48
55
 
49
56
  // 修改配置
50
57
  this.set_config(config);
51
-
58
+
52
59
  /**
53
- * 模板更目录
60
+ * 模板根目录
54
61
  */
55
- this.dir = "./template/"
62
+ this.dir = "./template/";
63
+
64
+ /**
65
+ * 当前主题
66
+ */
67
+ this.current_theme = this.config.defualt_theme;
68
+
69
+ /**
70
+ * 错误提示
71
+ */
72
+ this.error = null;
56
73
  }
57
74
  }
58
75
 
@@ -98,6 +115,33 @@ Tpl.prototype.render_before = function(body, model, options) {
98
115
  return ret;
99
116
  };
100
117
 
118
+ /**
119
+ * 加载函数
120
+ * @param {Object} m 模型
121
+ */
122
+ Tpl.prototype.loadFunc = function(m) {
123
+ var _this = this;
124
+ m.view = function(file, model, options) {
125
+ return _this.view(file, model, options);
126
+ }
127
+ m.render = function(body, model, options) {
128
+ return _this.render(body, model, options);
129
+ }
130
+ m.hook_action = function(name, content, ...args) {
131
+ if (!content) {
132
+ content = "";
133
+ }
134
+ return $.hook.runAction(name, content, ...args);
135
+ }
136
+ m.hook_filter = function(name, content, ...args) {
137
+ if (!content) {
138
+ content = "";
139
+ }
140
+ return $.hook.runFilter(name, content, ...args);
141
+ }
142
+ return m;
143
+ }
144
+
101
145
  /**
102
146
  * 渲染模板(主要)
103
147
  * @param {String} body 模板
@@ -108,10 +152,12 @@ Tpl.prototype.render_before = function(body, model, options) {
108
152
  Tpl.prototype.render_main = function(body, model, options) {
109
153
  var ret = "";
110
154
  if (body) {
111
- ret = template.render(body, Object.assign({}, {
155
+ var m = Object.assign({}, {
112
156
  viewBag: this.viewBag,
113
157
  globalBag: $.globalBag
114
- }, model), options);
158
+ }, model);
159
+ this.loadFunc(m);
160
+ ret = template.render(body, m, options);
115
161
  }
116
162
  return ret;
117
163
  };
@@ -176,15 +222,32 @@ Tpl.prototype.compile = function(body, options) {
176
222
  * @return {String} 渲染后的视图
177
223
  */
178
224
  Tpl.prototype.view = function(file, model, options) {
179
- if (file.replace('./', '').indexOf('.') == -1 && !file.endWith(this.config.extname)) {
225
+ if (file.replace('./', '').indexOf('.') === -1 && !file.endWith(this.config.extname)) {
180
226
  file += this.config.extname;
181
227
  }
182
- var f = file.fullname(this.dir);
228
+ var f = file.fullname(this.dir + this.current_theme);
229
+ if (!f.hasFile()) {
230
+ f = file.fullname(this.dir + this.config.defualt_theme);
231
+ if (!f.hasFile()) {
232
+ f = file.fullname(this.defualt_dir + this.config.current_theme);
233
+ if (!f.hasFile()) {
234
+ f = file.fullname(this.defualt_dir + this.config.defualt_theme);
235
+ }
236
+ }
237
+ }
238
+
239
+ if (!f.hasFile()) {
240
+ this.error = {
241
+ code: 10000,
242
+ message: f + "文件不存在!"
243
+ }
244
+ }
183
245
  var body = f.loadText();
184
246
  if (body) {
185
247
  return this.render(body, model, options);
248
+ } else {
249
+ return "";
186
250
  }
187
- return "";
188
251
  };
189
252
 
190
253
  /**
@@ -243,4 +306,4 @@ if (global.$) {
243
306
  $.tpl = new Tpl();
244
307
  }
245
308
 
246
- module.exports = Tpl;
309
+ module.exports = Tpl;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_tpl",
3
- "version": "2.1.3",
3
+ "version": "2.1.5",
4
4
  "description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,7 @@
1
+ <div class="bar_user">${@hook_filter('test', hook_action('test'))}</div>
2
+ <div class="bar">导航栏</div>
3
+ <style>
4
+ .bar {
5
+ font-weight: 800;
6
+ }
7
+ </style>
@@ -0,0 +1,11 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>${globalBag.name}</title>
6
+ </head>
7
+ <body>
8
+ <div>游戏主题</div>
9
+ <div>${@view('./bar.html')}</div>
10
+ </body>
11
+ </html>
package/test.js CHANGED
@@ -44,6 +44,24 @@ async function test() {
44
44
 
45
45
  // 设置实例化视图变量
46
46
  tpl.viewBag.name = "系统名称";
47
+ tpl.current_theme = "game";
48
+ tpl.dir = "";
49
+
50
+ $.hook.addAction('test', (ret) => {
51
+ return "<div>你好吗?</div>";
52
+ }, 2, "测试动作2");
53
+
54
+ $.hook.addAction('test', (ret) => {
55
+ return "<div>你好!</div>";
56
+ }, 1, "测试动作1");
57
+
58
+ $.hook.addFilter('test', (ret) => {
59
+ return ret.replace("你", "您");
60
+ }, 1, "测试过滤");
61
+
62
+ // $.hook.delAction('test', "测试动作1");
63
+ // $.hook.delFilter('test', "测试过滤");
64
+
47
65
  // 渲染视图(并缓存)
48
66
  var body = tpl.view("./test", { arr: [123, 234] }, {
49
67
  // 是否读取缓存文件, 如果为false, 但有cache_filename, 表示只保存缓存,但不读取
File without changes