mm_tpl 2.4.5 → 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 +141 -45
- package/cache/test.cache.html +19 -22
- package/hook.js +11 -10
- package/index.js +16 -15
- package/package.json +1 -1
- package/template/diy/ad.html +3 -39
- package/template/game/bar.html +5 -1
- package/template/game/test.html +1 -1
- package/test.js +18 -8
- package/template/defualt/test.html +0 -36
package/README.md
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
# mm_tpl
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
###
|
|
29
|
+
### 2. 初始化模板引擎
|
|
14
30
|
|
|
15
31
|
```javascript
|
|
16
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
|
-
###
|
|
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
|
-
###
|
|
61
|
+
### 4. 循环渲染
|
|
40
62
|
|
|
41
63
|
```javascript
|
|
42
64
|
// 数组循环
|
|
@@ -90,23 +112,32 @@ const eachResult = tpl.render(`
|
|
|
90
112
|
// </ul>
|
|
91
113
|
```
|
|
92
114
|
|
|
93
|
-
###
|
|
115
|
+
### 5. 视图渲染
|
|
94
116
|
|
|
95
117
|
```javascript
|
|
96
118
|
// 设置全局变量
|
|
97
119
|
$.globalBag.siteName = "我的网站";
|
|
98
120
|
|
|
99
121
|
// 设置视图变量
|
|
100
|
-
tpl.
|
|
122
|
+
tpl.set({
|
|
123
|
+
title: "首页",
|
|
124
|
+
keywords: "关键词"
|
|
125
|
+
});
|
|
101
126
|
|
|
102
127
|
// 渲染视图文件
|
|
103
128
|
const html = tpl.view("./views/index.html", {
|
|
104
129
|
user: { name: "张三" }
|
|
105
130
|
});
|
|
106
131
|
// 输出: 渲染结果 <div>Hello World</div>
|
|
132
|
+
|
|
133
|
+
// 获取视图变量
|
|
134
|
+
const title = tpl.get("title");
|
|
135
|
+
|
|
136
|
+
// 删除视图变量
|
|
137
|
+
tpl.del("keywords");
|
|
107
138
|
```
|
|
108
139
|
|
|
109
|
-
###
|
|
140
|
+
### 6. 缓存机制
|
|
110
141
|
|
|
111
142
|
```javascript
|
|
112
143
|
// 使用缓存渲染视图
|
|
@@ -117,11 +148,13 @@ const html = tpl.view("./views/page", {
|
|
|
117
148
|
cache_filename: "./cache" // 缓存文件名
|
|
118
149
|
});
|
|
119
150
|
|
|
120
|
-
//
|
|
121
|
-
tpl.
|
|
151
|
+
// 清除缓存
|
|
152
|
+
tpl.clearCache('./views/mall'); // 清除指定目录缓存
|
|
153
|
+
// 或
|
|
154
|
+
tpl.clearCache(); // 清除所有缓存
|
|
122
155
|
```
|
|
123
156
|
|
|
124
|
-
###
|
|
157
|
+
### 7. 钩子系统
|
|
125
158
|
|
|
126
159
|
#### 添加函数钩子
|
|
127
160
|
```javascript
|
|
@@ -132,7 +165,7 @@ $.hook.addFunc('list', async function(viewBag, req, type) {
|
|
|
132
165
|
viewBag.user_id = query.user_id;
|
|
133
166
|
}
|
|
134
167
|
return someData;
|
|
135
|
-
});
|
|
168
|
+
}, 100, 'listFunc'); // sort: 优先级, alias: 别名
|
|
136
169
|
```
|
|
137
170
|
|
|
138
171
|
#### 添加动作钩子
|
|
@@ -149,26 +182,45 @@ $.hook.addFilter('content', function(viewBag, content, param) {
|
|
|
149
182
|
}, 1, "contentFilter");
|
|
150
183
|
```
|
|
151
184
|
|
|
152
|
-
####
|
|
185
|
+
#### 执行钩子
|
|
153
186
|
```javascript
|
|
154
|
-
//
|
|
155
|
-
await tpl.runFunc('list', {
|
|
156
|
-
|
|
157
|
-
|
|
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
|
+
```
|
|
158
201
|
|
|
159
|
-
|
|
160
|
-
|
|
202
|
+
#### 删除钩子
|
|
203
|
+
```javascript
|
|
204
|
+
// 删除指定钩子
|
|
205
|
+
$.hook.delFunc('list', 'listFunc');
|
|
206
|
+
$.hook.delAction('header', 'headerAction');
|
|
207
|
+
$.hook.delFilter('content', 'contentFilter');
|
|
161
208
|
|
|
162
|
-
//
|
|
163
|
-
|
|
209
|
+
// 清空所有钩子
|
|
210
|
+
$.hook.clear();
|
|
164
211
|
```
|
|
165
212
|
|
|
166
213
|
## 高级特性
|
|
167
214
|
|
|
168
|
-
### 1.
|
|
215
|
+
### 1. 主题和目录支持
|
|
169
216
|
```javascript
|
|
170
217
|
// 设置当前主题
|
|
171
218
|
tpl.current_theme = "dark";
|
|
219
|
+
|
|
220
|
+
// 模板文件查找顺序(优先级从高到低):
|
|
221
|
+
// 1. diy目录(自定义目录)
|
|
222
|
+
// 2. 当前主题目录
|
|
223
|
+
// 3. 默认主题目录
|
|
172
224
|
```
|
|
173
225
|
|
|
174
226
|
### 2. 模板规则
|
|
@@ -183,36 +235,80 @@ tpl.current_theme = "dark";
|
|
|
183
235
|
```javascript
|
|
184
236
|
const html = tpl.view("./template.html", data);
|
|
185
237
|
if (tpl.error) {
|
|
186
|
-
|
|
238
|
+
console.error("渲染错误:", tpl.error);
|
|
187
239
|
}
|
|
188
240
|
```
|
|
189
241
|
|
|
190
|
-
### 4.
|
|
242
|
+
### 4. 字符转义
|
|
191
243
|
```javascript
|
|
192
|
-
//
|
|
193
|
-
tpl.
|
|
194
|
-
|
|
195
|
-
keywords: "关键词"
|
|
196
|
-
});
|
|
244
|
+
// 自动转义特殊字符
|
|
245
|
+
const escaped = tpl.escape("<div>${content}</div>");
|
|
246
|
+
// 输出: <div>·$·{content}</div>
|
|
197
247
|
|
|
198
|
-
//
|
|
199
|
-
const
|
|
248
|
+
// 还原转义字符
|
|
249
|
+
const restored = tpl.escape(escaped, true);
|
|
250
|
+
// 输出: <div>${content}</div>
|
|
251
|
+
```
|
|
200
252
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
//
|
|
204
|
-
|
|
253
|
+
### 5. 全局挂载
|
|
254
|
+
```javascript
|
|
255
|
+
// 自动挂载到全局对象
|
|
256
|
+
if (global.$ && !$.Tpl) {
|
|
257
|
+
$.Tpl = Tpl;
|
|
258
|
+
$.tpl = new Tpl();
|
|
259
|
+
}
|
|
205
260
|
```
|
|
206
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
|
+
|
|
207
292
|
## 注意事项
|
|
208
293
|
|
|
209
|
-
1.
|
|
210
|
-
2.
|
|
211
|
-
3.
|
|
212
|
-
4.
|
|
213
|
-
5.
|
|
214
|
-
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
|
+
```
|
|
215
311
|
|
|
216
312
|
## 许可证
|
|
217
313
|
|
|
218
|
-
MIT
|
|
314
|
+
MIT License
|
package/cache/test.cache.html
CHANGED
|
@@ -2,29 +2,26 @@
|
|
|
2
2
|
<html>
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="utf-8">
|
|
5
|
-
<title>超级美眉Art
|
|
5
|
+
<title>超级美眉Art模板引擎-游戏测试</title>
|
|
6
6
|
</head>
|
|
7
7
|
<body>
|
|
8
|
-
<
|
|
9
|
-
<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
var a_1 =234;
|
|
27
|
-
</script>系统名称计算
|
|
28
|
-
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>
|
|
29
26
|
</body>
|
|
30
27
|
</html>
|
package/hook.js
CHANGED
|
@@ -55,7 +55,7 @@ Hook.prototype._getName = function (func, alias) {
|
|
|
55
55
|
* @param {string} alias - 函数别名
|
|
56
56
|
* @returns {string} 函数别名
|
|
57
57
|
*/
|
|
58
|
-
Hook.prototype.addFunc = function (name, func, sort, alias) {
|
|
58
|
+
Hook.prototype.addFunc = function (name, func, sort = 100, alias = "") {
|
|
59
59
|
alias = this._getName(func, alias);
|
|
60
60
|
var obj = {
|
|
61
61
|
func,
|
|
@@ -185,14 +185,16 @@ Hook.prototype.delFilter = function (name, alias = "") {
|
|
|
185
185
|
Hook.prototype.runFunc = async function (name, model, req, ...args) {
|
|
186
186
|
var ret;
|
|
187
187
|
var list = this._func[name];
|
|
188
|
-
if (
|
|
189
|
-
var
|
|
190
|
-
for (var i = 0; i < len; i++) {
|
|
188
|
+
if (list) {
|
|
189
|
+
for (var i = 0; i < list.length; i++) {
|
|
191
190
|
var item = list[i];
|
|
192
191
|
try {
|
|
193
|
-
|
|
194
|
-
if (
|
|
195
|
-
|
|
192
|
+
var ret_new = item.func(model, req, ...args);
|
|
193
|
+
if (types.isPromise(ret_new)) {
|
|
194
|
+
ret_new = await ret_new;
|
|
195
|
+
if (ret_new) {
|
|
196
|
+
ret = ret_new;
|
|
197
|
+
}
|
|
196
198
|
}
|
|
197
199
|
} catch (err) {
|
|
198
200
|
$.log.error(`[${this.constructor.name}] 执行函数钩子失败 ${item.alias}`, err);
|
|
@@ -235,9 +237,8 @@ Hook.prototype.runAction = function (name, model, ...args) {
|
|
|
235
237
|
*/
|
|
236
238
|
Hook.prototype.runFilter = function (name, model, ret, ...args) {
|
|
237
239
|
var list = this._filter[name];
|
|
238
|
-
if (
|
|
239
|
-
var
|
|
240
|
-
for (var i = 0; i < len; i++) {
|
|
240
|
+
if (list) {
|
|
241
|
+
for (var i = 0; i < list.length; i++) {
|
|
241
242
|
var item = list[i];
|
|
242
243
|
try {
|
|
243
244
|
ret = item.func(model, ret, ...args) || ret;
|
package/index.js
CHANGED
|
@@ -346,17 +346,17 @@ Tpl.prototype.view = function (file, model, options) {
|
|
|
346
346
|
if (file.replace('./', '').indexOf('.') === -1 && !file.endsWith(this.config.extname)) {
|
|
347
347
|
file += this.config.extname;
|
|
348
348
|
}
|
|
349
|
-
var f = file.fullname(this.dir
|
|
349
|
+
var f = file.fullname('diy'.fullname(this.dir));
|
|
350
350
|
if (!f.hasFile()) {
|
|
351
|
-
f = file.fullname(this.config.default_dir
|
|
351
|
+
f = file.fullname('diy'.fullname(this.config.default_dir));
|
|
352
352
|
if (!f.hasFile()) {
|
|
353
|
-
f = file.fullname(this.
|
|
353
|
+
f = file.fullname(this.current_theme.fullname(this.dir));
|
|
354
354
|
if (!f.hasFile()) {
|
|
355
|
-
f = file.fullname(this.
|
|
355
|
+
f = file.fullname(this.config.default_theme.fullname(this.dir));
|
|
356
356
|
if (!f.hasFile()) {
|
|
357
|
-
f = file.fullname(this.config.default_dir
|
|
357
|
+
f = file.fullname(this.current_theme.fullname(this.config.default_dir));
|
|
358
358
|
if (!f.hasFile()) {
|
|
359
|
-
f = file.fullname(this.config.
|
|
359
|
+
f = file.fullname(this.config.default_theme.fullname(this.config.default_dir));
|
|
360
360
|
}
|
|
361
361
|
}
|
|
362
362
|
}
|
|
@@ -369,15 +369,16 @@ Tpl.prototype.view = function (file, model, options) {
|
|
|
369
369
|
message: f + "文件不存在!"
|
|
370
370
|
}
|
|
371
371
|
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
372
|
+
else {
|
|
373
|
+
var body = f.loadText();
|
|
374
|
+
if (body) {
|
|
375
|
+
body = this.escape(body);
|
|
376
|
+
body = this._renderSub(body, model, options);
|
|
377
|
+
body = this.escape(body, true);
|
|
378
|
+
return body;
|
|
379
|
+
}
|
|
380
380
|
}
|
|
381
|
+
return "";
|
|
381
382
|
};
|
|
382
383
|
|
|
383
384
|
/**
|
|
@@ -431,7 +432,7 @@ Tpl.prototype.clearCache = function (path, file) {
|
|
|
431
432
|
});
|
|
432
433
|
};
|
|
433
434
|
|
|
434
|
-
if (global.$ &&
|
|
435
|
+
if (global.$ && !$.Tpl) {
|
|
435
436
|
$.Tpl = Tpl;
|
|
436
437
|
$.tpl = new Tpl();
|
|
437
438
|
}
|
package/package.json
CHANGED
package/template/diy/ad.html
CHANGED
|
@@ -1,39 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
<
|
|
3
|
-
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<title>${globalBag.name}</title>
|
|
6
|
-
</head>
|
|
7
|
-
<body>
|
|
8
|
-
<h5>${viewBag.name}</h5>
|
|
9
|
-
<p>${@hookFilter('test', '你真的是人才!')}</p>
|
|
10
|
-
<p><!--{@hookFilter('test', '你真的是人才!', {key: '123'})}--></p>
|
|
11
|
-
<ul>
|
|
12
|
-
${loop arr value idx}
|
|
13
|
-
<li>\${ value }</li>
|
|
14
|
-
${/loop}
|
|
15
|
-
</ul>
|
|
16
|
-
<ul>
|
|
17
|
-
<!--[ for(var i = 0; i < arr.length; i++) { ]-->
|
|
18
|
-
<li>${ arr[i] }</li>
|
|
19
|
-
<!--[ } ]-->
|
|
20
|
-
</ul>
|
|
21
|
-
<ul>
|
|
22
|
-
<!--{loop arr value idx}-->
|
|
23
|
-
<li>${ value }</li>
|
|
24
|
-
<!--{/loop}-->
|
|
25
|
-
</ul>
|
|
26
|
-
<strong>${message}</strong>
|
|
27
|
-
<script type="text/javascript">
|
|
28
|
-
/*[loop arr value idx]*/
|
|
29
|
-
var a_${idx} = ${value};
|
|
30
|
-
/*[/loop]*/
|
|
31
|
-
</script>
|
|
32
|
-
${viewBag.name}
|
|
33
|
-
## "计算" ##
|
|
34
|
-
123123'"""#
|
|
35
|
-
"price" + "NIHAO"
|
|
36
|
-
#"""'
|
|
37
|
-
## "#计算" ##
|
|
38
|
-
</body>
|
|
39
|
-
</html>
|
|
1
|
+
<div>
|
|
2
|
+
<p>这是一个广告位!</p>
|
|
3
|
+
</div>
|
package/template/game/bar.html
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
<div class="bar_user">
|
|
2
|
-
${@hookFilter('test',
|
|
2
|
+
${@hookFilter('test', hookAction('test', '好的'), '真的')}
|
|
3
3
|
<div>用户ID:${viewBag.user_id}</div>
|
|
4
4
|
<div>当前项目ID:${viewBag.project_id}</div>
|
|
5
5
|
<div>${message}</div>
|
|
6
6
|
</div>
|
|
7
7
|
|
|
8
8
|
<div class="bar">导航栏</div>
|
|
9
|
+
<!-- 广告位 -->
|
|
10
|
+
<div class="ad">
|
|
11
|
+
${@view('./ad')}
|
|
12
|
+
</div>
|
|
9
13
|
<style>
|
|
10
14
|
.bar {
|
|
11
15
|
font-weight: 800;
|
package/template/game/test.html
CHANGED
package/test.js
CHANGED
|
@@ -5,7 +5,7 @@ const {Tpl} = require('./index.js');
|
|
|
5
5
|
* 演示简单的变量替换功能
|
|
6
6
|
* @async
|
|
7
7
|
*/
|
|
8
|
-
async function
|
|
8
|
+
async function test1() {
|
|
9
9
|
var tpl = new Tpl();
|
|
10
10
|
var body = tpl.render("<a>${test} + 123123</a>", {
|
|
11
11
|
test: 123
|
|
@@ -18,7 +18,7 @@ async function test() {
|
|
|
18
18
|
* 演示模板中的循环功能
|
|
19
19
|
* @async
|
|
20
20
|
*/
|
|
21
|
-
async function
|
|
21
|
+
async function test2() {
|
|
22
22
|
var tpl = new Tpl();
|
|
23
23
|
// 数组循环示例
|
|
24
24
|
// var body = tpl.render("<a>${loop arr value idx} ${idx} ${ value } ${/loop} + 123123</a>", { arr: [123,234] });
|
|
@@ -42,8 +42,7 @@ async function test() {
|
|
|
42
42
|
* 演示全局变量和视图渲染的使用
|
|
43
43
|
* @async
|
|
44
44
|
*/
|
|
45
|
-
async function
|
|
46
|
-
$.log.debug("测试开始")
|
|
45
|
+
async function test3() {
|
|
47
46
|
var tpl = new Tpl();
|
|
48
47
|
// 设置全局变量
|
|
49
48
|
$.globalBag.name = "超级美眉Art模板引擎";
|
|
@@ -62,7 +61,7 @@ async function test() {
|
|
|
62
61
|
* 演示带有动作和过滤器的钩子系统
|
|
63
62
|
* @async
|
|
64
63
|
*/
|
|
65
|
-
async function
|
|
64
|
+
async function test4() {
|
|
66
65
|
var tpl = new Tpl();
|
|
67
66
|
|
|
68
67
|
// 设置全局变量
|
|
@@ -75,7 +74,6 @@ async function test() {
|
|
|
75
74
|
|
|
76
75
|
// 添加列表函数钩子
|
|
77
76
|
$.hook.addFunc('list', async function(m, req, type) {
|
|
78
|
-
console.log("参数", m, req, type);
|
|
79
77
|
var query = req.query;
|
|
80
78
|
if (query.user_id) {
|
|
81
79
|
m.user_id = query.user_id;
|
|
@@ -115,7 +113,7 @@ async function test() {
|
|
|
115
113
|
}, 'article');
|
|
116
114
|
|
|
117
115
|
// 使用缓存渲染视图
|
|
118
|
-
var body = tpl.view("./
|
|
116
|
+
var body = tpl.view("./test", {
|
|
119
117
|
arr: [123, 234],
|
|
120
118
|
message: "hello world!"
|
|
121
119
|
}, {
|
|
@@ -130,4 +128,16 @@ async function test() {
|
|
|
130
128
|
}
|
|
131
129
|
|
|
132
130
|
// 执行测试
|
|
133
|
-
|
|
131
|
+
async function demo() {
|
|
132
|
+
$.log.debug("测试开始");
|
|
133
|
+
$.log.debug("测试1");
|
|
134
|
+
await test1();
|
|
135
|
+
$.log.debug("测试2");
|
|
136
|
+
await test2();
|
|
137
|
+
$.log.debug("测试3");
|
|
138
|
+
await test3();
|
|
139
|
+
$.log.debug("测试4");
|
|
140
|
+
await test4();
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
demo();
|
|
@@ -1,36 +0,0 @@
|
|
|
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>
|