mm_tpl 2.4.6 → 2.4.7
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 +13 -0
- package/README_EN.md +327 -0
- package/hook.js +1 -0
- package/index.js +1 -1
- package/package.json +25 -8
- package/.gitattributes +0 -5
- package/cache/test.cache.html +0 -27
- package/template/default/test.html +0 -36
- package/template/diy/ad.html +0 -3
- package/template/game/bar.html +0 -17
- package/template/game/test.html +0 -11
- package/test.js +0 -143
package/README.md
CHANGED
|
@@ -2,6 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
超级美眉模板引擎模块,基于art-template模板引擎,提供强大的MVC模式视图渲染功能,支持钩子系统、主题切换和缓存机制。
|
|
4
4
|
|
|
5
|
+
[English Documentation](README_EN.md) | [中文文档](README.md)
|
|
6
|
+
|
|
7
|
+
## 目录
|
|
8
|
+
|
|
9
|
+
- [特性](#特性)
|
|
10
|
+
- [安装](#安装)
|
|
11
|
+
- [快速开始](#快速开始)
|
|
12
|
+
- [高级特性](#高级特性)
|
|
13
|
+
- [API 参考](#api-参考)
|
|
14
|
+
- [注意事项](#注意事项)
|
|
15
|
+
- [开发](#开发)
|
|
16
|
+
- [许可证](#许可证)
|
|
17
|
+
|
|
5
18
|
## 特性
|
|
6
19
|
|
|
7
20
|
- 🚀 基于art-template高性能模板引擎
|
package/README_EN.md
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
# mm_tpl
|
|
2
|
+
|
|
3
|
+
Super Meimei Template Engine Module, based on art-template engine, provides powerful MVC pattern view rendering functionality with hook system, theme switching, and caching mechanism.
|
|
4
|
+
|
|
5
|
+
[中文文档](README.md) | [English Documentation](README_EN.md)
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Features](#features)
|
|
10
|
+
- [Installation](#installation)
|
|
11
|
+
- [Quick Start](#quick-start)
|
|
12
|
+
- [Advanced Features](#advanced-features)
|
|
13
|
+
- [API Reference](#api-reference)
|
|
14
|
+
- [Notes](#notes)
|
|
15
|
+
- [Development](#development)
|
|
16
|
+
- [License](#license)
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- 🚀 High-performance template engine based on art-template
|
|
21
|
+
- 🔧 Support for multiple template syntax rules
|
|
22
|
+
- 🎨 Theme switching and template file priority search
|
|
23
|
+
- ⚡ Built-in caching mechanism for performance optimization
|
|
24
|
+
- 🔌 Powerful hook system support
|
|
25
|
+
- 📁 Flexible directory structure support
|
|
26
|
+
- 🛡️ Comprehensive error handling mechanism
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
npm install mm_tpl
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Quick Start
|
|
35
|
+
|
|
36
|
+
### 1. Install Dependencies
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install mm_tpl
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### 2. Initialize Template Engine
|
|
43
|
+
|
|
44
|
+
```javascript
|
|
45
|
+
const {Tpl} = require('mm_tpl');
|
|
46
|
+
|
|
47
|
+
// Create template instance
|
|
48
|
+
const tpl = new Tpl({
|
|
49
|
+
root: process.cwd(), // Root directory
|
|
50
|
+
default_dir: "./template/", // Default template directory
|
|
51
|
+
default_theme: 'default', // Default theme
|
|
52
|
+
extname: ".html", // Template file extension
|
|
53
|
+
cache_root: './cache', // Cache directory
|
|
54
|
+
cache_extname: '.cache.html', // Cache file extension
|
|
55
|
+
rules: [nativeRule, artRule, htmlRule, jsRule, pyRule, pyRule2] // Template rules
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### 3. Basic Template Rendering
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
// Simple variable substitution
|
|
63
|
+
const result = tpl.render("<div>${message}</div>", {
|
|
64
|
+
message: "Hello World"
|
|
65
|
+
});
|
|
66
|
+
// Output: <div>Hello World</div>
|
|
67
|
+
|
|
68
|
+
// Compile template function
|
|
69
|
+
const compiled = tpl.compile("<div>${name} - ${age}</div>");
|
|
70
|
+
const result2 = compiled({ name: "John", age: 25 });
|
|
71
|
+
// Output: <div>John - 25</div>
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 4. Loop Rendering
|
|
75
|
+
|
|
76
|
+
```javascript
|
|
77
|
+
// Array loop
|
|
78
|
+
const arrayResult = tpl.render(`
|
|
79
|
+
<ul>
|
|
80
|
+
${loop arr value idx}
|
|
81
|
+
<li>${idx}: ${value}</li>
|
|
82
|
+
${/loop}
|
|
83
|
+
</ul>
|
|
84
|
+
`, {
|
|
85
|
+
arr: [123, 234]
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
// Object loop
|
|
89
|
+
const objectResult = tpl.render(`
|
|
90
|
+
<dl>
|
|
91
|
+
${loop obj value key}
|
|
92
|
+
<dt>${key}</dt>
|
|
93
|
+
<dd>${value}</dd>
|
|
94
|
+
${/loop}
|
|
95
|
+
</dl>
|
|
96
|
+
`, {
|
|
97
|
+
obj: { name: "John", age: 25 }
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Traditional for loop
|
|
101
|
+
const forResult = tpl.render(`
|
|
102
|
+
<ul>
|
|
103
|
+
<!--[ for(var i = 0; i < arr.length; i++){ ]-->
|
|
104
|
+
<li>${arr[i]}</li>
|
|
105
|
+
<!--[ } ]-->
|
|
106
|
+
</ul>
|
|
107
|
+
`, {
|
|
108
|
+
arr: [123, 234]
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Each loop
|
|
112
|
+
const eachResult = tpl.render(`
|
|
113
|
+
<ul>
|
|
114
|
+
${each arr}
|
|
115
|
+
<li>${$value}</li>
|
|
116
|
+
${/each}
|
|
117
|
+
</ul>
|
|
118
|
+
`, {
|
|
119
|
+
arr: [123, 234]
|
|
120
|
+
});
|
|
121
|
+
// Output:
|
|
122
|
+
// <ul>
|
|
123
|
+
// <li>123</li>
|
|
124
|
+
// <li>234</li>
|
|
125
|
+
// </ul>
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### 5. View Rendering
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
// Set global variables
|
|
132
|
+
$.globalBag.siteName = "My Website";
|
|
133
|
+
|
|
134
|
+
// Set view variables
|
|
135
|
+
tpl.set({
|
|
136
|
+
title: "Homepage",
|
|
137
|
+
keywords: "keywords"
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
// Render view file
|
|
141
|
+
const html = tpl.view("./views/index.html", {
|
|
142
|
+
user: { name: "John" }
|
|
143
|
+
});
|
|
144
|
+
// Output: Rendering result <div>Hello World</div>
|
|
145
|
+
|
|
146
|
+
// Get view variable
|
|
147
|
+
const title = tpl.get("title");
|
|
148
|
+
|
|
149
|
+
// Delete view variable
|
|
150
|
+
tpl.del("keywords");
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### 6. Caching Mechanism
|
|
154
|
+
|
|
155
|
+
```javascript
|
|
156
|
+
// Use cache for view rendering
|
|
157
|
+
const html = tpl.view("./views/page", {
|
|
158
|
+
data: someData
|
|
159
|
+
}, {
|
|
160
|
+
cache: true, // Whether to use cache
|
|
161
|
+
cache_filename: "./cache" // Cache filename
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Clear cache
|
|
165
|
+
tpl.clearCache('./views/mall'); // Clear cache for specific directory
|
|
166
|
+
// or
|
|
167
|
+
tpl.clearCache(); // Clear all cache
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### 7. Hook System
|
|
171
|
+
|
|
172
|
+
#### Add Function Hook
|
|
173
|
+
```javascript
|
|
174
|
+
$.hook.addFunc('list', async function(viewBag, req, type) {
|
|
175
|
+
// Process list logic
|
|
176
|
+
var query = req.query;
|
|
177
|
+
if (query.user_id) {
|
|
178
|
+
viewBag.user_id = query.user_id;
|
|
179
|
+
}
|
|
180
|
+
return someData;
|
|
181
|
+
}, 100, 'listFunc'); // sort: priority, alias: alias
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
#### Add Action Hook
|
|
185
|
+
```javascript
|
|
186
|
+
$.hook.addAction('header', function(viewBag, param) {
|
|
187
|
+
return "<header>Website Header</header>";
|
|
188
|
+
}, 1, "headerAction");
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### Add Filter Hook
|
|
192
|
+
```javascript
|
|
193
|
+
$.hook.addFilter('content', function(viewBag, content, param) {
|
|
194
|
+
return content.replace("old text", "new text");
|
|
195
|
+
}, 1, "contentFilter");
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### Execute Hooks
|
|
199
|
+
```javascript
|
|
200
|
+
// Execute function hook
|
|
201
|
+
const result = await tpl.runFunc('list', { query: { page: 1 } }, 'article');
|
|
202
|
+
|
|
203
|
+
// Execute action hook
|
|
204
|
+
const actionResult = tpl.runHook('header', { param: 'value' });
|
|
205
|
+
|
|
206
|
+
// Use hooks in template
|
|
207
|
+
const html = tpl.render(`
|
|
208
|
+
<div>
|
|
209
|
+
${hookAction('header')}
|
|
210
|
+
${hookFilter('content', 'original content')}
|
|
211
|
+
</div>
|
|
212
|
+
`, data);
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
#### Delete Hooks
|
|
216
|
+
```javascript
|
|
217
|
+
// Delete specific hook
|
|
218
|
+
$.hook.delFunc('list', 'listFunc');
|
|
219
|
+
$.hook.delAction('header', 'headerAction');
|
|
220
|
+
$.hook.delFilter('content', 'contentFilter');
|
|
221
|
+
|
|
222
|
+
// Clear all hooks
|
|
223
|
+
$.hook.clear();
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
## Advanced Features
|
|
227
|
+
|
|
228
|
+
### 1. Theme and Directory Support
|
|
229
|
+
```javascript
|
|
230
|
+
// Set current theme
|
|
231
|
+
tpl.current_theme = "dark";
|
|
232
|
+
|
|
233
|
+
// Template file search order (priority from high to low):
|
|
234
|
+
// 1. diy directory (custom directory)
|
|
235
|
+
// 2. Current theme directory
|
|
236
|
+
// 3. Default theme directory
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### 2. Template Rules
|
|
240
|
+
Built-in support for multiple template rules, including:
|
|
241
|
+
- nativeRule: Native syntax rules
|
|
242
|
+
- artRule: art-template syntax rules
|
|
243
|
+
- htmlRule: HTML comment syntax rules
|
|
244
|
+
- jsRule: JavaScript syntax rules
|
|
245
|
+
- pyRule: Python-style syntax rules
|
|
246
|
+
|
|
247
|
+
### 3. Error Handling
|
|
248
|
+
```javascript
|
|
249
|
+
const html = tpl.view("./template.html", data);
|
|
250
|
+
if (tpl.error) {
|
|
251
|
+
console.error("Rendering error:", tpl.error);
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### 4. Character Escaping
|
|
256
|
+
```javascript
|
|
257
|
+
// Automatically escape special characters
|
|
258
|
+
const escaped = tpl.escape("<div>${content}</div>");
|
|
259
|
+
// Output: <div>·$·{content}</div>
|
|
260
|
+
|
|
261
|
+
// Restore escaped characters
|
|
262
|
+
const restored = tpl.escape(escaped, true);
|
|
263
|
+
// Output: <div>${content}</div>
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### 5. Global Mounting
|
|
267
|
+
```javascript
|
|
268
|
+
// Automatically mount to global object
|
|
269
|
+
if (global.$ && !$.Tpl) {
|
|
270
|
+
$.Tpl = Tpl;
|
|
271
|
+
$.tpl = new Tpl();
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
## API Reference
|
|
276
|
+
|
|
277
|
+
### Tpl Class Methods
|
|
278
|
+
|
|
279
|
+
- `constructor(config)` - Constructor, create template instance
|
|
280
|
+
- `setConfig(config)` - Set configuration parameters
|
|
281
|
+
- `render(body, model, options)` - Render template string
|
|
282
|
+
- `compile(body, options)` - Compile template to function
|
|
283
|
+
- `view(file, model, options)` - Render template file
|
|
284
|
+
- `set(obj)` - Set view data
|
|
285
|
+
- `get(key)` - Get view data
|
|
286
|
+
- `del(keys)` - Delete view data
|
|
287
|
+
- `clearCache(path, file)` - Clear cache
|
|
288
|
+
- `runFunc(name, req, ...args)` - Execute function hook
|
|
289
|
+
- `runHook(name, req, ...args)` - Execute action hook (alias of runFunc)
|
|
290
|
+
- `escape(body, restore)` - Escape/restore characters
|
|
291
|
+
|
|
292
|
+
### Hook Class Methods
|
|
293
|
+
|
|
294
|
+
- `addFunc(name, func, sort, alias)` - Add function hook
|
|
295
|
+
- `addAction(name, func, sort, alias)` - Add action hook
|
|
296
|
+
- `addFilter(name, func, sort, alias)` - Add filter hook
|
|
297
|
+
- `runFunc(name, model, req, ...args)` - Execute function hook
|
|
298
|
+
- `runAction(name, model, ...args)` - Execute action hook
|
|
299
|
+
- `runFilter(name, model, ret, ...args)` - Execute filter hook
|
|
300
|
+
- `delFunc(name, alias)` - Delete function hook
|
|
301
|
+
- `delAction(name, alias)` - Delete action hook
|
|
302
|
+
- `delFilter(name, alias)` - Delete filter hook
|
|
303
|
+
- `clear(name)` - Clear hooks
|
|
304
|
+
|
|
305
|
+
## Notes
|
|
306
|
+
|
|
307
|
+
1. **Dependencies**: Requires `art-template` and `mm_expand` dependency packages
|
|
308
|
+
2. **Directory Permissions**: Ensure template directories have read/write permissions
|
|
309
|
+
3. **Cache Management**: Clean expired cache promptly to avoid memory leaks
|
|
310
|
+
4. **Hook Priority**: Lower numerical values indicate higher priority and earlier execution
|
|
311
|
+
5. **Async Processing**: Hook functions support async operations, pay attention to error handling
|
|
312
|
+
6. **File Search**: Template files are searched in priority order, ensure files exist
|
|
313
|
+
7. **Character Escaping**: Automatic handling of template syntax characters to avoid conflicts
|
|
314
|
+
|
|
315
|
+
## Development
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
# Run tests
|
|
319
|
+
npm test
|
|
320
|
+
|
|
321
|
+
# Check code coverage
|
|
322
|
+
npm run coverage
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
## License
|
|
326
|
+
|
|
327
|
+
MIT License
|
package/hook.js
CHANGED
package/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,27 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_tpl",
|
|
3
|
-
"version": "2.4.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.4.7",
|
|
4
|
+
"description": "Super Meimei Template Engine - High-performance template engine based on art-template with MVC pattern, hook system, theme switching and caching mechanism",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"dev": "nodemon test.js",
|
|
8
|
-
"test": "node test.js"
|
|
8
|
+
"test": "node test.js",
|
|
9
|
+
"coverage": "node --experimental-vm-modules node_modules/jest/bin/jest.js --coverage"
|
|
9
10
|
},
|
|
10
11
|
"repository": {
|
|
11
12
|
"type": "git",
|
|
12
13
|
"url": "git+https://gitee.com/qiuwenwu91/mm_tpl.git"
|
|
13
14
|
},
|
|
14
15
|
"keywords": [
|
|
16
|
+
"template",
|
|
17
|
+
"engine",
|
|
15
18
|
"mvc",
|
|
16
19
|
"web",
|
|
17
20
|
"view",
|
|
18
|
-
"model",
|
|
19
|
-
"controller",
|
|
20
21
|
"render",
|
|
21
|
-
"template"
|
|
22
|
+
"art-template",
|
|
23
|
+
"hook",
|
|
24
|
+
"theme",
|
|
25
|
+
"cache",
|
|
26
|
+
"nodejs",
|
|
27
|
+
"javascript"
|
|
22
28
|
],
|
|
23
29
|
"author": "邱文武",
|
|
24
|
-
"license": "
|
|
30
|
+
"license": "MIT",
|
|
25
31
|
"bugs": {
|
|
26
32
|
"url": "https://gitee.com/qiuwenwu91/mm_tpl/issues"
|
|
27
33
|
},
|
|
@@ -29,5 +35,16 @@
|
|
|
29
35
|
"dependencies": {
|
|
30
36
|
"art-template": "^4.13.4",
|
|
31
37
|
"mm_expand": "^1.9.7"
|
|
32
|
-
}
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=12.0.0"
|
|
41
|
+
},
|
|
42
|
+
"files": [
|
|
43
|
+
"index.js",
|
|
44
|
+
"hook.js",
|
|
45
|
+
"tpl_rule.js",
|
|
46
|
+
"README.md",
|
|
47
|
+
"README_EN.md",
|
|
48
|
+
"package.json"
|
|
49
|
+
]
|
|
33
50
|
}
|
package/.gitattributes
DELETED
package/cache/test.cache.html
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html>
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="utf-8">
|
|
5
|
-
<title>超级美眉Art模板引擎-游戏测试</title>
|
|
6
|
-
</head>
|
|
7
|
-
<body>
|
|
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>
|
|
26
|
-
</body>
|
|
27
|
-
</html>
|
|
@@ -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>
|
package/template/diy/ad.html
DELETED
package/template/game/bar.html
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
<div class="bar_user">
|
|
2
|
-
${@hookFilter('test', hookAction('test', '好的'), '真的')}
|
|
3
|
-
<div>用户ID:${viewBag.user_id}</div>
|
|
4
|
-
<div>当前项目ID:${viewBag.project_id}</div>
|
|
5
|
-
<div>${message}</div>
|
|
6
|
-
</div>
|
|
7
|
-
|
|
8
|
-
<div class="bar">导航栏</div>
|
|
9
|
-
<!-- 广告位 -->
|
|
10
|
-
<div class="ad">
|
|
11
|
-
${@view('./ad')}
|
|
12
|
-
</div>
|
|
13
|
-
<style>
|
|
14
|
-
.bar {
|
|
15
|
-
font-weight: 800;
|
|
16
|
-
}
|
|
17
|
-
</style>
|
package/template/game/test.html
DELETED
package/test.js
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
const {Tpl} = require('./index.js');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 基础模板渲染测试函数
|
|
5
|
-
* 演示简单的变量替换功能
|
|
6
|
-
* @async
|
|
7
|
-
*/
|
|
8
|
-
async function test1() {
|
|
9
|
-
var tpl = new Tpl();
|
|
10
|
-
var body = tpl.render("<a>${test} + 123123</a>", {
|
|
11
|
-
test: 123
|
|
12
|
-
});
|
|
13
|
-
$.log.debug(body);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 数组/对象循环渲染测试函数
|
|
18
|
-
* 演示模板中的循环功能
|
|
19
|
-
* @async
|
|
20
|
-
*/
|
|
21
|
-
async function test2() {
|
|
22
|
-
var tpl = new Tpl();
|
|
23
|
-
// 数组循环示例
|
|
24
|
-
// var body = tpl.render("<a>${loop arr value idx} ${idx} ${ value } ${/loop} + 123123</a>", { arr: [123,234] });
|
|
25
|
-
|
|
26
|
-
// 对象循环示例
|
|
27
|
-
var body = tpl.render("<a>${ loop obj value key } ${ key } ${ value } ${/loop} + 123123</a>", { obj: { a: 123, b: 467 } });
|
|
28
|
-
|
|
29
|
-
// 传统for循环示例
|
|
30
|
-
// var body = tpl.render("<a><!--[ for(var i = 0; i < arr.length; i++){ ]--> ${ arr[i] } <!--[ } ]--> + 123123</a>", {
|
|
31
|
-
// arr: [123, 234]
|
|
32
|
-
// });
|
|
33
|
-
|
|
34
|
-
// each循环示例
|
|
35
|
-
// var body = tpl.render("<a>${ each arr } ${ arr[$index] } ${/each} + 123123</a>", { arr: [123,234] });
|
|
36
|
-
// var view = tpl.render("<a>${ each arr } ${ $value } ${/each} + 123123</a>", { arr: [123,234] });
|
|
37
|
-
$.log.debug(body);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 模板视图渲染测试函数
|
|
42
|
-
* 演示全局变量和视图渲染的使用
|
|
43
|
-
* @async
|
|
44
|
-
*/
|
|
45
|
-
async function test3() {
|
|
46
|
-
var tpl = new Tpl();
|
|
47
|
-
// 设置全局变量
|
|
48
|
-
$.globalBag.name = "超级美眉Art模板引擎";
|
|
49
|
-
|
|
50
|
-
// 设置实例视图变量
|
|
51
|
-
tpl.viewBag.name = "系统名称";
|
|
52
|
-
|
|
53
|
-
// 渲染视图
|
|
54
|
-
var body = tpl.view("./test.html", { arr: [123,234] });
|
|
55
|
-
$.log.debug(tpl.error);
|
|
56
|
-
$.log.debug(body);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 钩子和动作测试函数
|
|
61
|
-
* 演示带有动作和过滤器的钩子系统
|
|
62
|
-
* @async
|
|
63
|
-
*/
|
|
64
|
-
async function test4() {
|
|
65
|
-
var tpl = new Tpl();
|
|
66
|
-
|
|
67
|
-
// 设置全局变量
|
|
68
|
-
$.globalBag.name = "超级美眉Art模板引擎";
|
|
69
|
-
|
|
70
|
-
// 设置实例视图变量
|
|
71
|
-
tpl.viewBag.name = "系统名称";
|
|
72
|
-
tpl.current_theme = "game";
|
|
73
|
-
tpl.dir = "";
|
|
74
|
-
|
|
75
|
-
// 添加列表函数钩子
|
|
76
|
-
$.hook.addFunc('list', async function(m, req, type) {
|
|
77
|
-
var query = req.query;
|
|
78
|
-
if (query.user_id) {
|
|
79
|
-
m.user_id = query.user_id;
|
|
80
|
-
}
|
|
81
|
-
if (query.project_id) {
|
|
82
|
-
m.project_id = query.project_id;
|
|
83
|
-
}
|
|
84
|
-
$.log.debug("列表", m);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
// 添加动作钩子
|
|
88
|
-
$.hook.addAction('test', function aa(m, param) {
|
|
89
|
-
return "<div>你好吗?</div>";
|
|
90
|
-
}, 2);
|
|
91
|
-
|
|
92
|
-
$.hook.addAction('test', function(m, param) {
|
|
93
|
-
return "<div>你好!}</div>";
|
|
94
|
-
}, 1, "测试动作1");
|
|
95
|
-
|
|
96
|
-
// 添加过滤器钩子
|
|
97
|
-
$.hook.addFilter('test', function(m, ret, param) {
|
|
98
|
-
$.log.debug("测试", ret);
|
|
99
|
-
return ret.replace("你", "您");
|
|
100
|
-
}, 1, "测试过滤");
|
|
101
|
-
|
|
102
|
-
// 删除动作钩子
|
|
103
|
-
$.hook.delAction('test', "aa");
|
|
104
|
-
$.hook.delAction('test', "测试动作1");
|
|
105
|
-
// $.hook.delFilter('test', "测试过滤");
|
|
106
|
-
|
|
107
|
-
// 运行函数钩子
|
|
108
|
-
await tpl.runFunc('list', {
|
|
109
|
-
query: {
|
|
110
|
-
user_id: 1,
|
|
111
|
-
project_id: 2
|
|
112
|
-
}
|
|
113
|
-
}, 'article');
|
|
114
|
-
|
|
115
|
-
// 使用缓存渲染视图
|
|
116
|
-
var body = tpl.view("./test", {
|
|
117
|
-
arr: [123, 234],
|
|
118
|
-
message: "hello world!"
|
|
119
|
-
}, {
|
|
120
|
-
// 是否读取缓存文件,如果为false但有cache_filename,则只保存缓存但不读取
|
|
121
|
-
cache: false,
|
|
122
|
-
// 缓存文件名
|
|
123
|
-
cache_filename: "./test"
|
|
124
|
-
});
|
|
125
|
-
// 删除指定目录下的缓存文件
|
|
126
|
-
// tpl.clear_cache('./mall');
|
|
127
|
-
$.log.debug("渲染结果", body);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// 执行测试
|
|
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();
|