mm_tpl 2.4.2 → 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 +211 -166
- package/cache/test.cache.html +1 -1
- package/package.json +6 -6
- package/template/defualt/test.html +36 -0
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
####
|
|
121
|
-
```javascript
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
query
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
```
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
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
|
package/cache/test.cache.html
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_tpl",
|
|
3
|
-
"version": "2.4.
|
|
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://
|
|
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://
|
|
26
|
+
"url": "https://gitee.com/qiuwenwu91/mm_tpl/issues"
|
|
27
27
|
},
|
|
28
|
-
"homepage": "https://
|
|
28
|
+
"homepage": "https://gitee.com/qiuwenwu91/mm_tpl#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"art-template": "^4.13.
|
|
31
|
-
"mm_expand": "^1.
|
|
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>
|