mm_tpl 2.4.3 → 2.4.5

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,212 +1,218 @@
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
-
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
+ // <ul>
88
+ // <li>123</li>
89
+ // <li>234</li>
90
+ // </ul>
91
+ ```
92
+
93
+ ### 4. 视图渲染
94
+
95
+ ```javascript
96
+ // 设置全局变量
97
+ $.globalBag.siteName = "我的网站";
98
+
99
+ // 设置视图变量
100
+ tpl.viewBag.title = "首页";
101
+
102
+ // 渲染视图文件
103
+ const html = tpl.view("./views/index.html", {
104
+ user: { name: "张三" }
105
+ });
106
+ // 输出: 渲染结果 <div>Hello World</div>
107
+ ```
108
+
109
+ ### 5. 缓存机制
110
+
111
+ ```javascript
112
+ // 使用缓存渲染视图
113
+ const html = tpl.view("./views/page", {
114
+ data: someData
115
+ }, {
116
+ cache: true, // 是否使用缓存
117
+ cache_filename: "./cache" // 缓存文件名
118
+ });
119
+
120
+ // 清除指定目录的缓存
121
+ tpl.clear_cache('./views/mall');
122
+ ```
123
+
124
+ ### 6. 钩子系统
125
+
126
+ #### 添加函数钩子
127
+ ```javascript
128
+ $.hook.addFunc('list', async function(viewBag, req, type) {
129
+ // 处理列表逻辑
130
+ var query = req.query;
131
+ if (query.user_id) {
132
+ viewBag.user_id = query.user_id;
133
+ }
134
+ return someData;
135
+ });
136
+ ```
137
+
138
+ #### 添加动作钩子
139
+ ```javascript
140
+ $.hook.addAction('header', function(viewBag, param) {
141
+ return "<header>网站头部</header>";
142
+ }, 1, "headerAction");
143
+ ```
144
+
145
+ #### 添加过滤器钩子
146
+ ```javascript
147
+ $.hook.addFilter('content', function(viewBag, content, param) {
148
+ return content.replace("旧文本", "新文本");
149
+ }, 1, "contentFilter");
150
+ ```
151
+
152
+ #### 在模板中使用钩子
153
+ ```javascript
154
+ // 运行函数钩子
155
+ await tpl.runFunc('list', {
156
+ query: { page: 1 }
157
+ }, 'article');
158
+
159
+ // 在模板中使用动作钩子
160
+ ${hook_action('header')}
161
+
162
+ // 在模板中使用过滤器钩子
163
+ ${hook_filter('content', '原始内容')}
164
+ ```
165
+
166
+ ## 高级特性
167
+
168
+ ### 1. 主题支持
169
+ ```javascript
170
+ // 设置当前主题
171
+ tpl.current_theme = "dark";
172
+ ```
173
+
174
+ ### 2. 模板规则
175
+ 模块内置支持多种模板规则,包括:
176
+ - nativeRule: 原生语法规则
177
+ - artRule: art-template语法规则
178
+ - htmlRule: HTML注释语法规则
179
+ - jsRule: JavaScript语法规则
180
+ - pyRule: Python风格语法规则
181
+
182
+ ### 3. 错误处理
183
+ ```javascript
184
+ const html = tpl.view("./template.html", data);
185
+ if (tpl.error) {
186
+ $.log.error("渲染错误:", tpl.error);
187
+ }
188
+ ```
189
+
190
+ ### 4. 视图背包操作
191
+ ```javascript
192
+ // 设置视图变量
193
+ tpl.set({
194
+ title: "页面标题",
195
+ keywords: "关键词"
196
+ });
197
+
198
+ // 获取视图变量
199
+ const title = tpl.get("title");
200
+
201
+ // 删除视图变量
202
+ tpl.del("title");
203
+ // 或批量删除
204
+ tpl.del(["title", "keywords"]);
205
+ ```
206
+
207
+ ## 注意事项
208
+
209
+ 1. 确保模板目录具有正确的读写权限
210
+ 2. 使用缓存时注意及时清理过期缓存
211
+ 3. 钩子的优先级数值越小,执行顺序越靠前
212
+ 4. 建议在生产环境开启模板缓存以提高性能
213
+ 5. 模板文件查找顺序:diy > current_theme > default_theme
214
+ 6. 使用钩子系统时注意处理异步操作
215
+
216
+ ## 许可证
217
+
212
218
  MIT
@@ -6,8 +6,8 @@
6
6
  </head>
7
7
  <body>
8
8
  <h5>系统名称</h5>
9
- <p>您真的是人才!</p>
10
- <p>您真的是人才!</p>
9
+ <p>你真的是人才!</p>
10
+ <p>你真的是人才!</p>
11
11
  <ul>
12
12
  <li>\·$·{· value }</li>
13
13
  <li>\·$·{· value }</li>
@@ -17,9 +17,10 @@
17
17
  <li>234</li>
18
18
  </ul>
19
19
  <ul>
20
- <li>\·$·{· value }</li>
21
- <li>\·$·{· value }</li>
20
+ <li>123</li>
21
+ <li>234</li>
22
22
  </ul>
23
+ <strong>hello world!</strong>
23
24
  <script type="text/javascript">
24
25
  var a_0 =123;
25
26
  var a_1 =234;