@zenweb/template 5.2.0 → 5.4.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/dist/index.js CHANGED
@@ -36,8 +36,8 @@ function contextTemplate(template_or_option) {
36
36
  }
37
37
  function setup(option) {
38
38
  return async function template(setup) {
39
- if (!option.engineName) {
40
- option.engineName = option.engine.name;
39
+ if (!option.renderName) {
40
+ option.renderName = option.render.name;
41
41
  }
42
42
  setup.debug('option: %o', option);
43
43
  setup.assertModuleExists('result', '@zenweb/result');
package/dist/render.js CHANGED
@@ -13,8 +13,8 @@ class TemplateRender {
13
13
  const opt = ctx.templateOption;
14
14
  if (opt) {
15
15
  // 匹配引擎
16
- if (opt.engine === this._option.engineName) {
17
- (0, utils_1.debug)('matchEngine: %o', this._option.engineName);
16
+ if (opt.render === this._option.renderName) {
17
+ (0, utils_1.debug)('matchRender: %o', this._option.renderName);
18
18
  return true;
19
19
  }
20
20
  // 指定模版
@@ -49,7 +49,7 @@ class TemplateRender {
49
49
  return true;
50
50
  }
51
51
  }
52
- return false;
52
+ return this._option.default || false;
53
53
  }
54
54
  render(ctx, data) {
55
55
  const opt = ctx.templateOption || {};
@@ -60,13 +60,16 @@ class TemplateRender {
60
60
  template = opt.failTemplate || this._option.failTemplate || template;
61
61
  }
62
62
  }
63
- if (!template) {
64
- template = ctx.path.slice(1);
63
+ if (!template && ctx.controller) {
64
+ template = `${ctx.controller.key}/${ctx.mapping.key}`;
65
65
  if (this._option.templateAffix) {
66
66
  template += this._option.templateAffix;
67
67
  }
68
68
  }
69
- return this._option.engine(template, data);
69
+ if (!template) {
70
+ throw new Error('No template specified');
71
+ }
72
+ return this._option.render(ctx, template, data);
70
73
  }
71
74
  }
72
75
  exports.TemplateRender = TemplateRender;
package/dist/types.d.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import { Context } from "zenweb";
1
2
  /**
2
3
  * 渲染引擎的渲染方法
3
4
  * - 方法名作为默认的引擎名称
4
5
  */
5
- export type TemplateEngine = (template: string, data?: any) => any;
6
+ export type TemplateRender = (ctx: Context, template: string, data?: any) => any;
6
7
  export interface TemplateSetupOption {
7
8
  /**
8
9
  * 输出类型
@@ -21,6 +22,12 @@ export interface TemplateSetupOption {
21
22
  * 匹配 Accept Type 头信息
22
23
  */
23
24
  matchAccept?: string[];
25
+ /**
26
+ * 默认使用
27
+ * - 设置为 true 的情况下在没有匹配任何规则下默认启用
28
+ * @default false
29
+ */
30
+ default?: boolean;
24
31
  /**
25
32
  * 模版名后缀
26
33
  * - 例如 '.html'
@@ -37,16 +44,16 @@ export interface TemplateSetupOption {
37
44
  /**
38
45
  * 渲染引擎
39
46
  */
40
- engine: TemplateEngine;
47
+ render: TemplateRender;
41
48
  /**
42
49
  * 设置渲染引擎名称
43
- * - 引擎名称默认取 `engine.name` 可以设置此项改名,可用于多模块切换
50
+ * - 引擎名称默认取 `render.name` 可以设置此项改名,可用于多模块切换
44
51
  */
45
- engineName?: string;
52
+ renderName?: string;
46
53
  }
47
54
  export interface TemplateOption {
48
55
  /**
49
- * 指定模版名,不指定取 `ctx.path`
56
+ * 指定模版名,不指定则自动根据 `控制器文件名/方法名` 生成
50
57
  */
51
58
  template?: string;
52
59
  /**
@@ -57,7 +64,7 @@ export interface TemplateOption {
57
64
  failTemplate?: string | false;
58
65
  /**
59
66
  * 使用的渲染引擎
60
- * - 对应的是 `TemplateSetupOption.engineName`
67
+ * - 对应的是 `TemplateSetupOption.renderName`
61
68
  */
62
- engine?: string;
69
+ render?: string;
63
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/template",
3
- "version": "5.2.0",
3
+ "version": "5.4.0",
4
4
  "description": "zenweb template render module",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -10,7 +10,6 @@
10
10
  "dev": "cd example && node --env-file=.env -r ts-node/register app.ts"
11
11
  },
12
12
  "files": [
13
- "AGENTS.md",
14
13
  "dist"
15
14
  ],
16
15
  "author": {
@@ -26,6 +25,7 @@
26
25
  "homepage": "https://zenweb.node.ltd",
27
26
  "devDependencies": {
28
27
  "@types/node": "^16.18.126",
28
+ "@zenweb/controller": "^6.7.0",
29
29
  "@zenweb/inject": "^5.4.0",
30
30
  "@zenweb/template-handlebars": "^4.1.0",
31
31
  "@zenweb/template-nunjucks": "^4.1.0",
package/AGENTS.md DELETED
@@ -1,187 +0,0 @@
1
- # @zenweb/template - AI API Reference
2
-
3
- AI 专用 API 参考文档,随 npm 包发布,供 AI 编程助手使用。
4
-
5
- ## 模块概述
6
-
7
- `@zenweb/template` 是 ZenWeb 框架的服务端模版渲染模块。集成模版引擎(如 Nunjucks、Handlebars)并根据请求条件(路径匹配、Accept 头、控制器显式指定)按需渲染输出。支持多个模版引擎实例共存。
8
-
9
- ## 导出
10
-
11
- ```ts
12
- export default function setup(option: TemplateSetupOption): SetupFunction;
13
- export { $template, TemplateSetupOption, TemplateOption, TemplateEngine };
14
- ```
15
-
16
- ## 类型
17
-
18
- ```ts
19
- /**
20
- * 渲染引擎方法签名
21
- * - 函数名 (name) 作为默认引擎名称
22
- */
23
- type TemplateEngine = (template: string, data?: any) => any;
24
-
25
- interface TemplateSetupOption {
26
- /** 输出类型,默认 'html' */
27
- type?: string;
28
- /** 匹配路径,例如 [/\.html$/i] */
29
- matchPath?: RegExp[];
30
- /** 忽略路径,例如 [/^\/api\//i] */
31
- ignorePath?: RegExp[];
32
- /** 匹配 Accept 头类型 */
33
- matchAccept?: string[];
34
- /**
35
- * 模版名后缀,例如 '.html'
36
- * 在不指定 TemplateOption.template 时有效
37
- */
38
- templateAffix?: string;
39
- /**
40
- * ctx.fail 操作使用的模版(全局)
41
- * 不指定则使用 TemplateOption.template 值
42
- * 控制器中可设置 TemplateOption.failTemplate = false 关闭
43
- */
44
- failTemplate?: string;
45
- /** 渲染引擎 */
46
- engine: TemplateEngine;
47
- /**
48
- * 自定义引擎名称
49
- * 默认取 engine.name,多引擎共存时用于区分
50
- */
51
- engineName?: string;
52
- }
53
-
54
- interface TemplateOption {
55
- /** 指定模版名,不指定取 ctx.path */
56
- template?: string;
57
- /**
58
- * 失败模版
59
- * 默认使用全局 failTemplate
60
- * 设为 false 强制使用 template
61
- */
62
- failTemplate?: string | false;
63
- /** 使用的引擎名称,对应 TemplateSetupOption.engineName */
64
- engine?: string;
65
- }
66
- ```
67
-
68
- ## setup(option) (default export)
69
-
70
- 模块安装函数,用于 `app.setup(modTemplate({ ... }))`。
71
-
72
- **处理流程:**
73
- 1. 设置引擎名称(默认取 `option.engine.name`)
74
- 2. 断言 `@zenweb/result` 模块已安装
75
- 3. 创建 `TemplateRender` 实例并注册到 `RenderManager`
76
- 4. 扩展 Context,挂载 `ctx.template()` 方法
77
-
78
- **基本用法:**
79
- ```ts
80
- import { create } from 'zenweb';
81
- import modTemplate from '@zenweb/template';
82
- import nunjucks from '@zenweb/template-nunjucks';
83
-
84
- create()
85
- .setup(modTemplate({
86
- matchPath: [/\.html$/i],
87
- engine: nunjucks(),
88
- }))
89
- .start();
90
- ```
91
-
92
- **多引擎配置:**
93
- ```ts
94
- create()
95
- .setup(modTemplate({
96
- matchPath: [/\.html$/i],
97
- engine: nunjucks(),
98
- }))
99
- .setup(modTemplate({
100
- type: 'xml',
101
- matchAccept: ['xml'],
102
- templateAffix: '.xml',
103
- engine: handlebars(),
104
- engineName: 'xml',
105
- }))
106
- .start();
107
- ```
108
-
109
- ## $template(template_or_option?)
110
-
111
- 全局方法,在控制器或服务中使用,基于 `@zenweb/core` 的 `$getContext()` 获取当前请求上下文。
112
-
113
- ```ts
114
- $template() // 启用模版渲染(使用默认匹配)
115
- $template('detail.html') // 指定模版文件
116
- $template({ failTemplate: 'error.html' }) // 指定失败模版
117
- $template({ engine: 'xml' }) // 切换引擎
118
- $template(false) // 关闭当前请求的模版渲染
119
- ```
120
-
121
- **控制器示例:**
122
- ```ts
123
- import { Context, mapping } from 'zenweb';
124
- import { $template } from '@zenweb/template';
125
-
126
- export class Controller {
127
- @mapping({ path: '/user.html' })
128
- user(ctx: Context) {
129
- $template(); // 启用渲染
130
- return { name: 'test' }; // 渲染 user.html 模版
131
- }
132
-
133
- @mapping({ path: '/api/data' })
134
- data() {
135
- $template(false); // 禁用渲染,返回 JSON
136
- return { name: 'test' };
137
- }
138
- }
139
- ```
140
-
141
- ## Context 挂载
142
-
143
- ```ts
144
- interface Context {
145
- /** 当前请求的模版渲染选项 */
146
- templateOption?: TemplateOption;
147
-
148
- /**
149
- * 启用并设置模版渲染
150
- * 不传参数: 启用
151
- * false: 关闭模版渲染
152
- * string: 设置模版文件名
153
- * TemplateOption: 详细设置
154
- */
155
- template(template_or_option?: false | string | TemplateOption): void;
156
- }
157
- ```
158
-
159
- ## 匹配规则
160
-
161
- `TemplateRender.match()` 按以下优先级判断请求是否需要模版渲染:
162
-
163
- 1. **引擎名匹配** — `ctx.templateOption.engine === option.engineName` 时直接匹配
164
- 2. **模版后缀匹配** — `ctx.templateOption.template` 以 `templateAffix` 结尾时匹配
165
- 3. **忽略路径** — `ignorePath` 中任一正则匹配 `ctx.path` 时返回 `false`
166
- 4. **路径匹配** — `matchPath` 中任一正则匹配 `ctx.path` 时匹配
167
- 5. **Accept 头匹配** — `matchAccept` 包含请求 Accept 类型时匹配
168
- 6. 以上均不满足则不匹配,回退到 JSON 渲染
169
-
170
- ## 渲染流程
171
-
172
- `TemplateRender.render()` 按以下逻辑确定模版名:
173
-
174
- 1. 若数据为 `ResultFail` 实例,包装为 `{ fail: data }`,并使用 `failTemplate`
175
- 2. 模版名优先级:`opt.template` → `ctx.path.slice(1) + templateAffix`
176
- 3. 调用 `engine(template, data)` 渲染输出
177
-
178
- ## 依赖模块
179
-
180
- - `@zenweb/result` — 必须在此之前安装,提供 `RenderManager` 和 `ResultRender` 接口
181
-
182
- ## 已知模版引擎适配器
183
-
184
- | 包名 | 引擎 |
185
- |------|------|
186
- | `@zenweb/template-nunjucks` | Nunjucks |
187
- | `@zenweb/template-handlebars` | Handlebars |