mm_tpl 2.4.1 → 2.4.3

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,167 +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
-
66
- ### 4. 视图渲染
67
-
68
- ```javascript
69
- // 设置全局变量
70
- $.globalBag.siteName = "我的网站";
71
-
72
- // 设置视图变量
73
- tpl.viewBag.title = "首页";
74
-
75
- // 渲染视图文件
76
- const html = tpl.view("./views/index.html", {
77
- user: { name: "张三" }
78
- });
79
- ```
80
-
81
- ### 5. 缓存机制
82
-
83
- ```javascript
84
- // 使用缓存渲染视图
85
- const html = tpl.view("./views/page", {
86
- data: someData
87
- }, {
88
- cache: true, // 是否使用缓存
89
- cache_filename: "./cache" // 缓存文件名
90
- });
91
-
92
- // 清除指定目录的缓存
93
- tpl.clear_cache('./views/mall');
94
- ```
95
-
96
- ### 6. 钩子系统
97
-
98
- #### 添加函数钩子
99
- ```javascript
100
- $.hook.addFunc('list', async function(viewBag, req, type) {
101
- // 处理列表逻辑
102
- return someData;
103
- });
104
- ```
105
-
106
- #### 添加动作钩子
107
- ```javascript
108
- $.hook.addAction('header', function(viewBag, param) {
109
- return "<header>网站头部</header>";
110
- }, 1, "headerAction");
111
- ```
112
-
113
- #### 添加过滤器钩子
114
- ```javascript
115
- $.hook.addFilter('content', function(viewBag, content, param) {
116
- return content.replace("旧文本", "新文本");
117
- }, 1, "contentFilter");
118
- ```
119
-
120
- #### 使用钩子
121
- ```javascript
122
- // 运行函数钩子
123
- await tpl.runFunc('list', {
124
- query: { page: 1 }
125
- }, 'article');
126
-
127
- // 在模板中使用动作钩子
128
- ${hook_action('header')}
129
-
130
- // 在模板中使用过滤器钩子
131
- ${hook_filter('content', '原始内容')}
132
- ```
133
-
134
- ## 高级特性
135
-
136
- ### 1. 主题支持
137
- ```javascript
138
- // 设置当前主题
139
- tpl.current_theme = "dark";
140
- ```
141
-
142
- ### 2. 模板规则
143
- 模块内置支持多种模板规则,包括:
144
- - nativeRule: 原生语法规则
145
- - artRule: art-template语法规则
146
- - htmlRule: HTML注释语法规则
147
- - jsRule: JavaScript语法规则
148
- - pyRule: Python风格语法规则
149
-
150
- ### 3. 错误处理
151
- ```javascript
152
- const html = tpl.view("./template.html", data);
153
- if (tpl.error) {
154
- console.error("渲染错误:", tpl.error);
155
- }
156
- ```
157
-
158
- ## 注意事项
159
-
160
- 1. 确保模板目录具有正确的读写权限
161
- 2. 使用缓存时注意及时清理过期缓存
162
- 3. 钩子的优先级数值越小,执行顺序越靠前
163
- 4. 建议在生产环境开启模板缓存以提高性能
164
-
165
- ## 许可证
166
-
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
+
167
212
  MIT
@@ -0,0 +1,29 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>超级美眉Art模板引擎</title>
6
+ </head>
7
+ <body>
8
+ <h5>系统名称</h5>
9
+ <p>您真的是人才!</p>
10
+ <p>您真的是人才!</p>
11
+ <ul>
12
+ <li>\·$·{· value }</li>
13
+ <li>\·$·{· value }</li>
14
+ </ul>
15
+ <ul>
16
+ <li>123</li>
17
+ <li>234</li>
18
+ </ul>
19
+ <ul>
20
+ <li>\·$·{· value }</li>
21
+ <li>\·$·{· value }</li>
22
+ </ul>
23
+ <script type="text/javascript">
24
+ var a_0 =123;
25
+ var a_1 =234;
26
+ </script>系统名称计算
27
+ 123123'priceNIHAO'#计算
28
+ </body>
29
+ </html>
package/index.js CHANGED
@@ -205,11 +205,23 @@ Tpl.prototype.render_before = function(body, model, options) {
205
205
  return ret;
206
206
  };
207
207
 
208
+ /**
209
+ * 用于注入全局函数
210
+ * @param {Object} m
211
+ */
212
+ Tpl.prototype.loadFunc_before = function(m) {
213
+ // m.$i18n = function(key) {
214
+ // return $.i18n(key);
215
+ // }
216
+ return m;
217
+ }
218
+
208
219
  /**
209
220
  * 加载函数
210
221
  * @param {Object} m 模型
211
222
  */
212
223
  Tpl.prototype.loadFunc = function(m) {
224
+ this.loadFunc_before(m);
213
225
  var _this = this;
214
226
  m.view = function(file, model, options) {
215
227
  return _this.view(file, Object.assign({}, m, model), options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_tpl",
3
- "version": "2.4.1",
3
+ "version": "2.4.3",
4
4
  "description": "这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,7 +9,7 @@
9
9
  },
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "git+https://github.com/qiuwenwu/mm_tpl.git"
12
+ "url": "git+https://gitee.com/qiuwenwu91/mm_tpl.git"
13
13
  },
14
14
  "keywords": [
15
15
  "mvc",
@@ -23,11 +23,11 @@
23
23
  "author": "邱文武",
24
24
  "license": "ISC",
25
25
  "bugs": {
26
- "url": "https://github.com/qiuwenwu/mm_tpl/issues"
26
+ "url": "https://gitee.com/qiuwenwu91/mm_tpl/issues"
27
27
  },
28
- "homepage": "https://github.com/qiuwenwu/mm_tpl#readme",
28
+ "homepage": "https://gitee.com/qiuwenwu91/mm_tpl#readme",
29
29
  "dependencies": {
30
- "art-template": "^4.13.2",
31
- "mm_expand": "^1.7.9"
30
+ "art-template": "^4.13.4",
31
+ "mm_expand": "^1.8.3"
32
32
  }
33
33
  }
@@ -0,0 +1,36 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <title>${globalBag.name}</title>
6
+ </head>
7
+ <body>
8
+ <h5>${viewBag.name}</h5>
9
+ <ul>
10
+ ${loop arr value idx}
11
+ <li>${ value }</li>
12
+ ${/loop}
13
+ </ul>
14
+ <ul>
15
+ <!--[ for(var i = 0; i < arr.length; i++) { ]-->
16
+ <li>${ arr[i] }</li>
17
+ <!--[ } ]-->
18
+ </ul>
19
+ <ul>
20
+ <!--{loop arr value idx}-->
21
+ <li>${ value }</li>
22
+ <!--{/loop}-->
23
+ </ul>
24
+ <script type="text/javascript">
25
+ /*[loop arr value idx]*/
26
+ var a_${idx} = ${value};
27
+ /*[/loop]*/
28
+ </script>
29
+ ${viewBag.name}
30
+ ## "计算" ##
31
+ 123123'"""#
32
+ "price" + "NIHAO"
33
+ #"""'
34
+ ## "#计算" ##
35
+ </body>
36
+ </html>
package/test.js CHANGED
@@ -91,7 +91,7 @@ async function test() {
91
91
  }, 2);
92
92
 
93
93
  $.hook.addAction('test', function(m, param) {
94
- return "<div>你好!</div>";
94
+ return "<div>你好!}</div>";
95
95
  }, 1, "测试动作1");
96
96
 
97
97
  // 添加过滤器钩子