mm_tpl 2.4.4 → 2.4.6

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,6 +1,16 @@
1
1
  # mm_tpl
2
2
 
3
- 这是超级美眉模板帮助类模块,用于MVC模式下的视图渲染。基于art-template模板引擎,提供了更强大的模板渲染功能。
3
+ 超级美眉模板引擎模块,基于art-template模板引擎,提供强大的MVC模式视图渲染功能,支持钩子系统、主题切换和缓存机制。
4
+
5
+ ## 特性
6
+
7
+ - 🚀 基于art-template高性能模板引擎
8
+ - 🔧 支持多种模板语法规则
9
+ - 🎨 主题切换和模板文件优先级搜索
10
+ - ⚡ 内置缓存机制提升性能
11
+ - 🔌 强大的钩子系统支持
12
+ - 📁 灵活的目录结构支持
13
+ - 🛡️ 完善的错误处理机制
4
14
 
5
15
  ## 安装
6
16
 
@@ -8,35 +18,47 @@
8
18
  npm install mm_tpl
9
19
  ```
10
20
 
11
- ## 基本使用
21
+ ## 快速开始
22
+
23
+ ### 1. 安装依赖
24
+
25
+ ```bash
26
+ npm install mm_tpl
27
+ ```
12
28
 
13
- ### 1. 初始化模板引擎
29
+ ### 2. 初始化模板引擎
14
30
 
15
31
  ```javascript
16
- const Tpl = require('mm_tpl');
32
+ const {Tpl} = require('mm_tpl');
33
+
34
+ // 创建模板实例
17
35
  const tpl = new Tpl({
18
- // 可选配置参数
19
36
  root: process.cwd(), // 根目录
20
37
  default_dir: "./template/", // 默认模板目录
21
38
  default_theme: 'default', // 默认主题
22
39
  extname: ".html", // 模板文件扩展名
23
40
  cache_root: './cache', // 缓存目录
24
- cache_extname: '.cache.html' // 缓存文件扩展名
41
+ cache_extname: '.cache.html', // 缓存文件扩展名
42
+ rules: [nativeRule, artRule, htmlRule, jsRule, pyRule, pyRule2] // 模板规则
25
43
  });
26
44
  ```
27
45
 
28
- ### 2. 基础模板渲染
46
+ ### 3. 基础模板渲染
29
47
 
30
48
  ```javascript
31
49
  // 简单变量替换
32
50
  const result = tpl.render("<div>${message}</div>", {
33
51
  message: "Hello World"
34
52
  });
35
-
36
53
  // 输出: <div>Hello World</div>
54
+
55
+ // 编译模板函数
56
+ const compiled = tpl.compile("<div>${name} - ${age}</div>");
57
+ const result2 = compiled({ name: "张三", age: 25 });
58
+ // 输出: <div>张三 - 25</div>
37
59
  ```
38
60
 
39
- ### 3. 循环渲染
61
+ ### 4. 循环渲染
40
62
 
41
63
  ```javascript
42
64
  // 数组循环
@@ -83,24 +105,39 @@ const eachResult = tpl.render(`
83
105
  `, {
84
106
  arr: [123, 234]
85
107
  });
108
+ // 输出:
109
+ // <ul>
110
+ // <li>123</li>
111
+ // <li>234</li>
112
+ // </ul>
86
113
  ```
87
114
 
88
- ### 4. 视图渲染
115
+ ### 5. 视图渲染
89
116
 
90
117
  ```javascript
91
118
  // 设置全局变量
92
119
  $.globalBag.siteName = "我的网站";
93
120
 
94
121
  // 设置视图变量
95
- tpl.viewBag.title = "首页";
122
+ tpl.set({
123
+ title: "首页",
124
+ keywords: "关键词"
125
+ });
96
126
 
97
127
  // 渲染视图文件
98
128
  const html = tpl.view("./views/index.html", {
99
129
  user: { name: "张三" }
100
130
  });
131
+ // 输出: 渲染结果 <div>Hello World</div>
132
+
133
+ // 获取视图变量
134
+ const title = tpl.get("title");
135
+
136
+ // 删除视图变量
137
+ tpl.del("keywords");
101
138
  ```
102
139
 
103
- ### 5. 缓存机制
140
+ ### 6. 缓存机制
104
141
 
105
142
  ```javascript
106
143
  // 使用缓存渲染视图
@@ -111,11 +148,13 @@ const html = tpl.view("./views/page", {
111
148
  cache_filename: "./cache" // 缓存文件名
112
149
  });
113
150
 
114
- // 清除指定目录的缓存
115
- tpl.clear_cache('./views/mall');
151
+ // 清除缓存
152
+ tpl.clearCache('./views/mall'); // 清除指定目录缓存
153
+ // 或
154
+ tpl.clearCache(); // 清除所有缓存
116
155
  ```
117
156
 
118
- ### 6. 钩子系统
157
+ ### 7. 钩子系统
119
158
 
120
159
  #### 添加函数钩子
121
160
  ```javascript
@@ -126,7 +165,7 @@ $.hook.addFunc('list', async function(viewBag, req, type) {
126
165
  viewBag.user_id = query.user_id;
127
166
  }
128
167
  return someData;
129
- });
168
+ }, 100, 'listFunc'); // sort: 优先级, alias: 别名
130
169
  ```
131
170
 
132
171
  #### 添加动作钩子
@@ -143,26 +182,45 @@ $.hook.addFilter('content', function(viewBag, content, param) {
143
182
  }, 1, "contentFilter");
144
183
  ```
145
184
 
146
- #### 在模板中使用钩子
185
+ #### 执行钩子
147
186
  ```javascript
148
- // 运行函数钩子
149
- await tpl.runFunc('list', {
150
- query: { page: 1 }
151
- }, 'article');
187
+ // 执行函数钩子
188
+ const result = await tpl.runFunc('list', { query: { page: 1 } }, 'article');
189
+
190
+ // 执行动作钩子
191
+ const actionResult = tpl.runHook('header', { param: 'value' });
192
+
193
+ // 在模板中使用钩子
194
+ const html = tpl.render(`
195
+ <div>
196
+ ${hookAction('header')}
197
+ ${hookFilter('content', '原始内容')}
198
+ </div>
199
+ `, data);
200
+ ```
152
201
 
153
- // 在模板中使用动作钩子
154
- ${hook_action('header')}
202
+ #### 删除钩子
203
+ ```javascript
204
+ // 删除指定钩子
205
+ $.hook.delFunc('list', 'listFunc');
206
+ $.hook.delAction('header', 'headerAction');
207
+ $.hook.delFilter('content', 'contentFilter');
155
208
 
156
- // 在模板中使用过滤器钩子
157
- ${hook_filter('content', '原始内容')}
209
+ // 清空所有钩子
210
+ $.hook.clear();
158
211
  ```
159
212
 
160
213
  ## 高级特性
161
214
 
162
- ### 1. 主题支持
215
+ ### 1. 主题和目录支持
163
216
  ```javascript
164
217
  // 设置当前主题
165
218
  tpl.current_theme = "dark";
219
+
220
+ // 模板文件查找顺序(优先级从高到低):
221
+ // 1. diy目录(自定义目录)
222
+ // 2. 当前主题目录
223
+ // 3. 默认主题目录
166
224
  ```
167
225
 
168
226
  ### 2. 模板规则
@@ -177,36 +235,80 @@ tpl.current_theme = "dark";
177
235
  ```javascript
178
236
  const html = tpl.view("./template.html", data);
179
237
  if (tpl.error) {
180
- $.log.error("渲染错误:", tpl.error);
238
+ console.error("渲染错误:", tpl.error);
181
239
  }
182
240
  ```
183
241
 
184
- ### 4. 视图背包操作
242
+ ### 4. 字符转义
185
243
  ```javascript
186
- // 设置视图变量
187
- tpl.set({
188
- title: "页面标题",
189
- keywords: "关键词"
190
- });
244
+ // 自动转义特殊字符
245
+ const escaped = tpl.escape("<div>${content}</div>");
246
+ // 输出: <div>·$·{content}</div>
191
247
 
192
- // 获取视图变量
193
- const title = tpl.get("title");
248
+ // 还原转义字符
249
+ const restored = tpl.escape(escaped, true);
250
+ // 输出: <div>${content}</div>
251
+ ```
194
252
 
195
- // 删除视图变量
196
- tpl.del("title");
197
- // 或批量删除
198
- tpl.del(["title", "keywords"]);
253
+ ### 5. 全局挂载
254
+ ```javascript
255
+ // 自动挂载到全局对象
256
+ if (global.$ && !$.Tpl) {
257
+ $.Tpl = Tpl;
258
+ $.tpl = new Tpl();
259
+ }
199
260
  ```
200
261
 
262
+ ## API 参考
263
+
264
+ ### Tpl 类方法
265
+
266
+ - `constructor(config)` - 构造函数,创建模板实例
267
+ - `setConfig(config)` - 设置配置参数
268
+ - `render(body, model, options)` - 渲染模板字符串
269
+ - `compile(body, options)` - 编译模板为函数
270
+ - `view(file, model, options)` - 渲染模板文件
271
+ - `set(obj)` - 设置视图数据
272
+ - `get(key)` - 获取视图数据
273
+ - `del(keys)` - 删除视图数据
274
+ - `clearCache(path, file)` - 清除缓存
275
+ - `runFunc(name, req, ...args)` - 执行函数钩子
276
+ - `runHook(name, req, ...args)` - 执行动作钩子(runFunc别名)
277
+ - `escape(body, restore)` - 转义/还原字符
278
+
279
+ ### Hook 类方法
280
+
281
+ - `addFunc(name, func, sort, alias)` - 添加函数钩子
282
+ - `addAction(name, func, sort, alias)` - 添加动作钩子
283
+ - `addFilter(name, func, sort, alias)` - 添加过滤器钩子
284
+ - `runFunc(name, model, req, ...args)` - 执行函数钩子
285
+ - `runAction(name, model, ...args)` - 执行动作钩子
286
+ - `runFilter(name, model, ret, ...args)` - 执行过滤器钩子
287
+ - `delFunc(name, alias)` - 删除函数钩子
288
+ - `delAction(name, alias)` - 删除动作钩子
289
+ - `delFilter(name, alias)` - 删除过滤器钩子
290
+ - `clear(name)` - 清空钩子
291
+
201
292
  ## 注意事项
202
293
 
203
- 1. 确保模板目录具有正确的读写权限
204
- 2. 使用缓存时注意及时清理过期缓存
205
- 3. 钩子的优先级数值越小,执行顺序越靠前
206
- 4. 建议在生产环境开启模板缓存以提高性能
207
- 5. 模板文件查找顺序:diy > current_theme > default_theme
208
- 6. 使用钩子系统时注意处理异步操作
294
+ 1. **依赖要求**: 需要安装 `art-template` 和 `mm_expand` 依赖包
295
+ 2. **目录权限**: 确保模板目录具有读写权限
296
+ 3. **缓存管理**: 及时清理过期缓存,避免内存泄漏
297
+ 4. **钩子优先级**: 数值越小优先级越高,执行顺序越靠前
298
+ 5. **异步处理**: 钩子函数支持异步操作,注意错误处理
299
+ 6. **文件查找**: 模板文件按优先级顺序查找,确保文件存在
300
+ 7. **字符转义**: 自动处理模板语法字符,避免冲突
301
+
302
+ ## 开发
303
+
304
+ ```bash
305
+ # 运行测试
306
+ npm test
307
+
308
+ # 查看代码覆盖率
309
+ npm run coverage
310
+ ```
209
311
 
210
312
  ## 许可证
211
313
 
212
- MIT
314
+ MIT License
@@ -2,28 +2,26 @@
2
2
  <html>
3
3
  <head>
4
4
  <meta charset="utf-8">
5
- <title>超级美眉Art模板引擎</title>
5
+ <title>超级美眉Art模板引擎-游戏测试</title>
6
6
  </head>
7
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'#计算
8
+ <div>游戏主题</div>
9
+ <div><div class="bar_user">
10
+ <div>用户ID:1</div>
11
+ <div>当前项目ID:2</div>
12
+ <div>hello world!</div>
13
+ </div>
14
+
15
+ <div class="bar">导航栏</div>
16
+ <!-- 广告位 -->
17
+ <div class="ad"><div>
18
+ <p>这是一个广告位!</p>
19
+ </div>
20
+ </div>
21
+ <style>
22
+ .bar {
23
+ font-weight: 800;
24
+ }
25
+ </style></div>
28
26
  </body>
29
27
  </html>