@zenweb/template 4.1.0 → 5.0.0
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 +44 -50
- package/dist/global.d.ts +9 -0
- package/dist/global.js +15 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils.d.ts +1 -2
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -1,33 +1,23 @@
|
|
|
1
|
-
#
|
|
1
|
+
# template - 服务端模版渲染
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## 功能说明
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
在服务器端使用模版引擎渲染模版并根据请求条件按需输出
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
yarn add @zenweb/template
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
### nunjucks
|
|
12
|
-
|
|
13
|
-
```bash
|
|
14
|
-
yarn add @zenweb/template-nunjucks
|
|
15
|
-
```
|
|
7
|
+
## 演示
|
|
16
8
|
|
|
17
|
-
###
|
|
9
|
+
### 安装
|
|
18
10
|
|
|
19
11
|
```bash
|
|
20
|
-
yarn add @zenweb/template-
|
|
12
|
+
yarn add @zenweb/template @zenweb/template-nunjucks
|
|
21
13
|
```
|
|
22
14
|
|
|
23
|
-
|
|
15
|
+
### 配置
|
|
24
16
|
|
|
25
|
-
src/index.ts
|
|
26
17
|
```ts
|
|
27
18
|
import { create } from 'zenweb';
|
|
28
19
|
import modTemplate from '@zenweb/template';
|
|
29
20
|
import nunjucks from '@zenweb/template-nunjucks';
|
|
30
|
-
import handlebars from '@zenweb/template-handlebars';
|
|
31
21
|
|
|
32
22
|
create()
|
|
33
23
|
|
|
@@ -39,55 +29,59 @@ create()
|
|
|
39
29
|
}),
|
|
40
30
|
}))
|
|
41
31
|
|
|
42
|
-
// Multiple Engines
|
|
43
|
-
.setup(modTemplate({
|
|
44
|
-
type: 'xml',
|
|
45
|
-
matchPath: [/\.xml$/i],
|
|
46
|
-
templateAffix: '.xml',
|
|
47
|
-
engineName: 'xml',
|
|
48
|
-
engine: handlebars(),
|
|
49
|
-
}))
|
|
50
|
-
|
|
51
32
|
.start();
|
|
52
33
|
```
|
|
53
34
|
|
|
54
|
-
|
|
35
|
+
控制器使用
|
|
55
36
|
|
|
56
|
-
src/controller/demo.ts
|
|
57
37
|
```ts
|
|
58
|
-
import { Context, mapping } from
|
|
38
|
+
import { Context, mapping, Body, $body } from 'zenweb';
|
|
59
39
|
|
|
60
|
-
export class
|
|
61
|
-
@mapping({ path:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
40
|
+
export class Controller {
|
|
41
|
+
@mapping({ path: '/', method: 'POST' })
|
|
42
|
+
post(body: Body) {
|
|
43
|
+
console.log(body.type); // POST body 内容类型
|
|
44
|
+
console.log(body.data); // POST Body 内容解析完成后的数据
|
|
65
45
|
}
|
|
66
46
|
|
|
67
|
-
@mapping({ path: '/
|
|
68
|
-
|
|
69
|
-
|
|
47
|
+
@mapping({ path: '/', method: 'POST' })
|
|
48
|
+
async post() {
|
|
49
|
+
console.log(await getAge()); // 类型转换&校验
|
|
70
50
|
}
|
|
51
|
+
}
|
|
71
52
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
ctx.template({ engine: 'xml' })
|
|
75
|
-
return { name: 'Test' };
|
|
76
|
-
}
|
|
53
|
+
async function getAge() {
|
|
54
|
+
return (await $body.get({ age: '!int' })).age;
|
|
77
55
|
}
|
|
78
56
|
```
|
|
79
57
|
|
|
58
|
+
模版代码
|
|
59
|
+
|
|
80
60
|
template/index.html
|
|
81
61
|
```html
|
|
82
62
|
<h1>Hello {{name}}</h1>
|
|
83
63
|
```
|
|
84
64
|
|
|
85
|
-
|
|
86
|
-
```html
|
|
87
|
-
<h1>Hello {{name}}</h1>
|
|
88
|
-
```
|
|
65
|
+
## 依赖模块
|
|
89
66
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
67
|
+
- @zenweb/result
|
|
68
|
+
|
|
69
|
+
## Core 挂载项
|
|
70
|
+
|
|
71
|
+
无
|
|
72
|
+
|
|
73
|
+
## Context 挂载项
|
|
74
|
+
|
|
75
|
+
- `templateOption`
|
|
76
|
+
- `template(option)` 设置当前请求的模版渲染选项
|
|
77
|
+
|
|
78
|
+
## 全局模式
|
|
79
|
+
|
|
80
|
+
| 方法 | 功能 |
|
|
81
|
+
| ----- | ---- |
|
|
82
|
+
| $template() | 设置当前请求的模版渲染选项 |
|
|
83
|
+
|
|
84
|
+
## 目前已支持的模版引擎
|
|
85
|
+
|
|
86
|
+
[nunjucks](https://www.npmjs.com/package/@zenweb/template-nunjucks)
|
|
87
|
+
[handlebars](https://www.npmjs.com/package/@zenweb/template-handlebars)
|
package/dist/global.d.ts
ADDED
package/dist/global.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.$template = void 0;
|
|
4
|
+
const core_1 = require("@zenweb/core");
|
|
5
|
+
/**
|
|
6
|
+
* 启用并设置模版渲染
|
|
7
|
+
* - 不传参数: 启用
|
|
8
|
+
* - false: 关闭模版渲染
|
|
9
|
+
* - string: 设置模版文件名
|
|
10
|
+
* - TemplateOption: 详细设置
|
|
11
|
+
*/
|
|
12
|
+
function $template(template_or_option) {
|
|
13
|
+
return (0, core_1.$getContext)().template(template_or_option);
|
|
14
|
+
}
|
|
15
|
+
exports.$template = $template;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SetupFunction } from '@zenweb/core';
|
|
2
2
|
import { TemplateSetupOption, TemplateOption } from './types';
|
|
3
3
|
export * from './types';
|
|
4
|
+
export * from './global';
|
|
4
5
|
export default function setup(option: TemplateSetupOption): SetupFunction;
|
|
5
6
|
declare module '@zenweb/core' {
|
|
6
7
|
interface Context {
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
17
17
|
const result_1 = require("@zenweb/result");
|
|
18
18
|
const render_1 = require("./render");
|
|
19
19
|
__exportStar(require("./types"), exports);
|
|
20
|
+
__exportStar(require("./global"), exports);
|
|
20
21
|
function contextTemplate(template_or_option) {
|
|
21
22
|
if (template_or_option === false) {
|
|
22
23
|
this.templateOption = undefined;
|
package/dist/utils.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/template",
|
|
3
3
|
"packageManager": "yarn@4.0.2",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "5.0.0",
|
|
5
5
|
"description": "zenweb template render module",
|
|
6
6
|
"exports": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
@@ -27,17 +27,17 @@
|
|
|
27
27
|
"homepage": "https://zenweb.node.ltd",
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^20.10.6",
|
|
30
|
-
"@zenweb/inject": "^
|
|
30
|
+
"@zenweb/inject": "^5.0.1",
|
|
31
31
|
"@zenweb/template-handlebars": "^3.0.0",
|
|
32
32
|
"@zenweb/template-nunjucks": "^3.3.0",
|
|
33
33
|
"cross-env": "^7.0.3",
|
|
34
34
|
"rimraf": "^4.4.1",
|
|
35
35
|
"ts-node": "^10.9.1",
|
|
36
36
|
"typescript": "~5.3.3",
|
|
37
|
-
"zenweb": "^
|
|
37
|
+
"zenweb": "^5.0.0"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@zenweb/core": "^
|
|
41
|
-
"@zenweb/result": "^4.
|
|
40
|
+
"@zenweb/core": "^5.0.0",
|
|
41
|
+
"@zenweb/result": "^4.1.3"
|
|
42
42
|
}
|
|
43
43
|
}
|