mm_tpl 2.4.3 → 2.4.4

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
@@ -1,212 +1,212 @@
1
- # mm_tpl
2
-
3
- 这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染。基于art-template模板引擎,提供了更强大的模板渲染功能。
4
-
5
- ## 安装
6
-
7
- ```bash
8
- npm install mm_tpl
9
- ```
10
-
11
- ## 基本使用
12
-
13
- ### 1. 初始化模板引擎
14
-
15
- ```javascript
16
- const Tpl = require('mm_tpl');
17
- const tpl = new Tpl({
18
- // 可选配置参数
19
- root: process.cwd(), // 根目录
20
- default_dir: "./template/", // 默认模板目录
21
- default_theme: 'default', // 默认主题
22
- extname: ".html", // 模板文件扩展名
23
- cache_root: './cache', // 缓存目录
24
- cache_extname: '.cache.html' // 缓存文件扩展名
25
- });
26
- ```
27
-
28
- ### 2. 基础模板渲染
29
-
30
- ```javascript
31
- // 简单变量替换
32
- const result = tpl.render("<div>${message}</div>", {
33
- message: "Hello World"
34
- });
35
-
36
- // 输出: <div>Hello World</div>
37
- ```
38
-
39
- ### 3. 循环渲染
40
-
41
- ```javascript
42
- // 数组循环
43
- const arrayResult = tpl.render(`
44
- <ul>
45
- ${loop arr value idx}
46
- <li>${idx}: ${value}</li>
47
- ${/loop}
48
- </ul>
49
- `, {
50
- arr: [123, 234]
51
- });
52
-
53
- // 对象循环
54
- const objectResult = tpl.render(`
55
- <dl>
56
- ${loop obj value key}
57
- <dt>${key}</dt>
58
- <dd>${value}</dd>
59
- ${/loop}
60
- </dl>
61
- `, {
62
- obj: { name: "张三", age: 25 }
63
- });
64
-
65
- // 传统for循环
66
- const forResult = tpl.render(`
67
- <ul>
68
- <!--[ for(var i = 0; i < arr.length; i++){ ]-->
69
- <li>${arr[i]}</li>
70
- <!--[ } ]-->
71
- </ul>
72
- `, {
73
- arr: [123, 234]
74
- });
75
-
76
- // each循环
77
- const eachResult = tpl.render(`
78
- <ul>
79
- ${each arr}
80
- <li>${$value}</li>
81
- ${/each}
82
- </ul>
83
- `, {
84
- arr: [123, 234]
85
- });
86
- ```
87
-
88
- ### 4. 视图渲染
89
-
90
- ```javascript
91
- // 设置全局变量
92
- $.globalBag.siteName = "我的网站";
93
-
94
- // 设置视图变量
95
- tpl.viewBag.title = "首页";
96
-
97
- // 渲染视图文件
98
- const html = tpl.view("./views/index.html", {
99
- user: { name: "张三" }
100
- });
101
- ```
102
-
103
- ### 5. 缓存机制
104
-
105
- ```javascript
106
- // 使用缓存渲染视图
107
- const html = tpl.view("./views/page", {
108
- data: someData
109
- }, {
110
- cache: true, // 是否使用缓存
111
- cache_filename: "./cache" // 缓存文件名
112
- });
113
-
114
- // 清除指定目录的缓存
115
- tpl.clear_cache('./views/mall');
116
- ```
117
-
118
- ### 6. 钩子系统
119
-
120
- #### 添加函数钩子
121
- ```javascript
122
- $.hook.addFunc('list', async function(viewBag, req, type) {
123
- // 处理列表逻辑
124
- var query = req.query;
125
- if (query.user_id) {
126
- viewBag.user_id = query.user_id;
127
- }
128
- return someData;
129
- });
130
- ```
131
-
132
- #### 添加动作钩子
133
- ```javascript
134
- $.hook.addAction('header', function(viewBag, param) {
135
- return "<header>网站头部</header>";
136
- }, 1, "headerAction");
137
- ```
138
-
139
- #### 添加过滤器钩子
140
- ```javascript
141
- $.hook.addFilter('content', function(viewBag, content, param) {
142
- return content.replace("旧文本", "新文本");
143
- }, 1, "contentFilter");
144
- ```
145
-
146
- #### 在模板中使用钩子
147
- ```javascript
148
- // 运行函数钩子
149
- await tpl.runFunc('list', {
150
- query: { page: 1 }
151
- }, 'article');
152
-
153
- // 在模板中使用动作钩子
154
- ${hook_action('header')}
155
-
156
- // 在模板中使用过滤器钩子
157
- ${hook_filter('content', '原始内容')}
158
- ```
159
-
160
- ## 高级特性
161
-
162
- ### 1. 主题支持
163
- ```javascript
164
- // 设置当前主题
165
- tpl.current_theme = "dark";
166
- ```
167
-
168
- ### 2. 模板规则
169
- 模块内置支持多种模板规则,包括:
170
- - nativeRule: 原生语法规则
171
- - artRule: art-template语法规则
172
- - htmlRule: HTML注释语法规则
173
- - jsRule: JavaScript语法规则
174
- - pyRule: Python风格语法规则
175
-
176
- ### 3. 错误处理
177
- ```javascript
178
- const html = tpl.view("./template.html", data);
179
- if (tpl.error) {
180
- console.error("渲染错误:", tpl.error);
181
- }
182
- ```
183
-
184
- ### 4. 视图背包操作
185
- ```javascript
186
- // 设置视图变量
187
- tpl.set({
188
- title: "页面标题",
189
- keywords: "关键词"
190
- });
191
-
192
- // 获取视图变量
193
- const title = tpl.get("title");
194
-
195
- // 删除视图变量
196
- tpl.del("title");
197
- // 或批量删除
198
- tpl.del(["title", "keywords"]);
199
- ```
200
-
201
- ## 注意事项
202
-
203
- 1. 确保模板目录具有正确的读写权限
204
- 2. 使用缓存时注意及时清理过期缓存
205
- 3. 钩子的优先级数值越小,执行顺序越靠前
206
- 4. 建议在生产环境开启模板缓存以提高性能
207
- 5. 模板文件查找顺序:diy > current_theme > default_theme
208
- 6. 使用钩子系统时注意处理异步操作
209
-
210
- ## 许可证
211
-
1
+ # mm_tpl
2
+
3
+ 这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染。基于art-template模板引擎,提供了更强大的模板渲染功能。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install mm_tpl
9
+ ```
10
+
11
+ ## 基本使用
12
+
13
+ ### 1. 初始化模板引擎
14
+
15
+ ```javascript
16
+ const Tpl = require('mm_tpl');
17
+ const tpl = new Tpl({
18
+ // 可选配置参数
19
+ root: process.cwd(), // 根目录
20
+ default_dir: "./template/", // 默认模板目录
21
+ default_theme: 'default', // 默认主题
22
+ extname: ".html", // 模板文件扩展名
23
+ cache_root: './cache', // 缓存目录
24
+ cache_extname: '.cache.html' // 缓存文件扩展名
25
+ });
26
+ ```
27
+
28
+ ### 2. 基础模板渲染
29
+
30
+ ```javascript
31
+ // 简单变量替换
32
+ const result = tpl.render("<div>${message}</div>", {
33
+ message: "Hello World"
34
+ });
35
+
36
+ // 输出: <div>Hello World</div>
37
+ ```
38
+
39
+ ### 3. 循环渲染
40
+
41
+ ```javascript
42
+ // 数组循环
43
+ const arrayResult = tpl.render(`
44
+ <ul>
45
+ ${loop arr value idx}
46
+ <li>${idx}: ${value}</li>
47
+ ${/loop}
48
+ </ul>
49
+ `, {
50
+ arr: [123, 234]
51
+ });
52
+
53
+ // 对象循环
54
+ const objectResult = tpl.render(`
55
+ <dl>
56
+ ${loop obj value key}
57
+ <dt>${key}</dt>
58
+ <dd>${value}</dd>
59
+ ${/loop}
60
+ </dl>
61
+ `, {
62
+ obj: { name: "张三", age: 25 }
63
+ });
64
+
65
+ // 传统for循环
66
+ const forResult = tpl.render(`
67
+ <ul>
68
+ <!--[ for(var i = 0; i < arr.length; i++){ ]-->
69
+ <li>${arr[i]}</li>
70
+ <!--[ } ]-->
71
+ </ul>
72
+ `, {
73
+ arr: [123, 234]
74
+ });
75
+
76
+ // each循环
77
+ const eachResult = tpl.render(`
78
+ <ul>
79
+ ${each arr}
80
+ <li>${$value}</li>
81
+ ${/each}
82
+ </ul>
83
+ `, {
84
+ arr: [123, 234]
85
+ });
86
+ ```
87
+
88
+ ### 4. 视图渲染
89
+
90
+ ```javascript
91
+ // 设置全局变量
92
+ $.globalBag.siteName = "我的网站";
93
+
94
+ // 设置视图变量
95
+ tpl.viewBag.title = "首页";
96
+
97
+ // 渲染视图文件
98
+ const html = tpl.view("./views/index.html", {
99
+ user: { name: "张三" }
100
+ });
101
+ ```
102
+
103
+ ### 5. 缓存机制
104
+
105
+ ```javascript
106
+ // 使用缓存渲染视图
107
+ const html = tpl.view("./views/page", {
108
+ data: someData
109
+ }, {
110
+ cache: true, // 是否使用缓存
111
+ cache_filename: "./cache" // 缓存文件名
112
+ });
113
+
114
+ // 清除指定目录的缓存
115
+ tpl.clear_cache('./views/mall');
116
+ ```
117
+
118
+ ### 6. 钩子系统
119
+
120
+ #### 添加函数钩子
121
+ ```javascript
122
+ $.hook.addFunc('list', async function(viewBag, req, type) {
123
+ // 处理列表逻辑
124
+ var query = req.query;
125
+ if (query.user_id) {
126
+ viewBag.user_id = query.user_id;
127
+ }
128
+ return someData;
129
+ });
130
+ ```
131
+
132
+ #### 添加动作钩子
133
+ ```javascript
134
+ $.hook.addAction('header', function(viewBag, param) {
135
+ return "<header>网站头部</header>";
136
+ }, 1, "headerAction");
137
+ ```
138
+
139
+ #### 添加过滤器钩子
140
+ ```javascript
141
+ $.hook.addFilter('content', function(viewBag, content, param) {
142
+ return content.replace("旧文本", "新文本");
143
+ }, 1, "contentFilter");
144
+ ```
145
+
146
+ #### 在模板中使用钩子
147
+ ```javascript
148
+ // 运行函数钩子
149
+ await tpl.runFunc('list', {
150
+ query: { page: 1 }
151
+ }, 'article');
152
+
153
+ // 在模板中使用动作钩子
154
+ ${hook_action('header')}
155
+
156
+ // 在模板中使用过滤器钩子
157
+ ${hook_filter('content', '原始内容')}
158
+ ```
159
+
160
+ ## 高级特性
161
+
162
+ ### 1. 主题支持
163
+ ```javascript
164
+ // 设置当前主题
165
+ tpl.current_theme = "dark";
166
+ ```
167
+
168
+ ### 2. 模板规则
169
+ 模块内置支持多种模板规则,包括:
170
+ - nativeRule: 原生语法规则
171
+ - artRule: art-template语法规则
172
+ - htmlRule: HTML注释语法规则
173
+ - jsRule: JavaScript语法规则
174
+ - pyRule: Python风格语法规则
175
+
176
+ ### 3. 错误处理
177
+ ```javascript
178
+ const html = tpl.view("./template.html", data);
179
+ if (tpl.error) {
180
+ $.log.error("渲染错误:", tpl.error);
181
+ }
182
+ ```
183
+
184
+ ### 4. 视图背包操作
185
+ ```javascript
186
+ // 设置视图变量
187
+ tpl.set({
188
+ title: "页面标题",
189
+ keywords: "关键词"
190
+ });
191
+
192
+ // 获取视图变量
193
+ const title = tpl.get("title");
194
+
195
+ // 删除视图变量
196
+ tpl.del("title");
197
+ // 或批量删除
198
+ tpl.del(["title", "keywords"]);
199
+ ```
200
+
201
+ ## 注意事项
202
+
203
+ 1. 确保模板目录具有正确的读写权限
204
+ 2. 使用缓存时注意及时清理过期缓存
205
+ 3. 钩子的优先级数值越小,执行顺序越靠前
206
+ 4. 建议在生产环境开启模板缓存以提高性能
207
+ 5. 模板文件查找顺序:diy > current_theme > default_theme
208
+ 6. 使用钩子系统时注意处理异步操作
209
+
210
+ ## 许可证
211
+
212
212
  MIT
package/hook.js CHANGED
@@ -165,7 +165,7 @@ Hook.prototype.runAction = function(name, model, ...args) {
165
165
  try {
166
166
  ret += o.func(model, ...args);
167
167
  } catch (err) {
168
- console.error(ret);
168
+ $.log.error(ret);
169
169
  }
170
170
  }
171
171
  }
@@ -188,7 +188,7 @@ Hook.prototype.runFilter = function(name, model, ret, ...args) {
188
188
  try {
189
189
  ret = o.func(model, ret, ...args);
190
190
  } catch (err) {
191
- console.error(ret);
191
+ $.log.error(ret);
192
192
  }
193
193
  }
194
194
  }
@@ -215,7 +215,7 @@ Hook.prototype.runFunc = async function(name, model, req, ...args) {
215
215
  ret = await ret;
216
216
  }
217
217
  } catch (err) {
218
- console.error(ret);
218
+ $.log.error(ret);
219
219
  }
220
220
  }
221
221
  }
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- require('mm_expand');
1
+ require('mm_logs');
2
2
  const template = require('art-template');
3
3
  const Hook = require('./hook.js');
4
4
  const {
@@ -55,7 +55,7 @@ class Tpl {
55
55
  this.viewBag = {};
56
56
 
57
57
  // 修改配置
58
- this.set_config(config);
58
+ this.setConfig(config);
59
59
 
60
60
  /**
61
61
  * 模板根目录
@@ -89,7 +89,7 @@ class Tpl {
89
89
  * 修改配置
90
90
  * @param {Object} config 配置参数
91
91
  */
92
- Tpl.prototype.set_config = function(config) {
92
+ Tpl.prototype.setConfig = function(config) {
93
93
  if (config) {
94
94
  this.config = Object.assign(this.config, config);
95
95
  }
@@ -125,7 +125,7 @@ Tpl.prototype.init_before = function(config) {}
125
125
  * @param {Object} config 配置参数
126
126
  */
127
127
  Tpl.prototype.init_main = function(config) {
128
- this.set_config(config);
128
+ this.setConfig(config);
129
129
  }
130
130
 
131
131
  /**
@@ -291,7 +291,7 @@ Tpl.prototype.render_after = function(body, model, options) {
291
291
  * @param {Object} options 配置参数
292
292
  * @return {String} view 渲染后的视图
293
293
  */
294
- Tpl.prototype.render_sub = function(body, model, options) {
294
+ Tpl.prototype.renderSub = function(body, model, options) {
295
295
  var ret = this.render_before(body, model, options);
296
296
  if (!ret) {
297
297
  ret = this.render_main(body, model, options);
@@ -311,7 +311,7 @@ Tpl.prototype.render_sub = function(body, model, options) {
311
311
  */
312
312
  Tpl.prototype.render = function(body, model, options) {
313
313
  body = this.change(body);
314
- body = this.render_sub(body, model, options);
314
+ body = this.renderSub(body, model, options);
315
315
  return this.change(body, true);
316
316
  };
317
317
 
@@ -362,7 +362,7 @@ Tpl.prototype.view = function(file, model, options) {
362
362
  var body = f.loadText();
363
363
  if (body) {
364
364
  body = this.change(body);
365
- body = this.render_sub(body, model, options);
365
+ body = this.renderSub(body, model, options);
366
366
  body = this.change(body, true);
367
367
  return body;
368
368
  } else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_tpl",
3
- "version": "2.4.3",
3
+ "version": "2.4.4",
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_logs": "^1.5.3"
32
32
  }
33
33
  }
package/test.js CHANGED
@@ -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
  /**
@@ -82,7 +82,7 @@ async function test() {
82
82
  if (query.project_id) {
83
83
  m.project_id = query.project_id;
84
84
  }
85
- console.log("列表", m);
85
+ $.log.debug("列表", m);
86
86
  });
87
87
 
88
88
  // 添加动作钩子
@@ -96,7 +96,7 @@ async function test() {
96
96
 
97
97
  // 添加过滤器钩子
98
98
  $.hook.addFilter('test', function(m, ret, param) {
99
- console.log("测试", ret);
99
+ $.log.debug("测试", ret);
100
100
  return ret.replace("你", "您");
101
101
  }, 1, "测试过滤");
102
102
 
@@ -125,7 +125,7 @@ async function test() {
125
125
  });
126
126
  // 删除指定目录下的缓存文件
127
127
  // tpl.clear_cache('./mall');
128
- console.log(body);
128
+ $.log.debug(body);
129
129
  }
130
130
 
131
131
  // 执行测试
package/tpl_rule.js CHANGED
@@ -51,7 +51,7 @@ var artRule = {
51
51
 
52
52
  // 旧版语法升级提示
53
53
  var warn = function warn(oldSyntax, newSyntax) {
54
- console.warn((options.filename || 'anonymous') + ':' + (match.line + 1) + ':' + (match.start + 1) + '\n' + (
54
+ $.log.warn((options.filename || 'anonymous') + ':' + (match.line + 1) + ':' + (match.start + 1) + '\n' + (
55
55
  'Template upgrade: ${' + oldSyntax + '} -> ${' + newSyntax + '}'));
56
56
  };
57
57