@zenweb/template 5.3.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
  // 指定模版
@@ -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
  * 输出类型
@@ -43,16 +44,16 @@ export interface TemplateSetupOption {
43
44
  /**
44
45
  * 渲染引擎
45
46
  */
46
- engine: TemplateEngine;
47
+ render: TemplateRender;
47
48
  /**
48
49
  * 设置渲染引擎名称
49
- * - 引擎名称默认取 `engine.name` 可以设置此项改名,可用于多模块切换
50
+ * - 引擎名称默认取 `render.name` 可以设置此项改名,可用于多模块切换
50
51
  */
51
- engineName?: string;
52
+ renderName?: string;
52
53
  }
53
54
  export interface TemplateOption {
54
55
  /**
55
- * 指定模版名,不指定取 `ctx.path`
56
+ * 指定模版名,不指定则自动根据 `控制器文件名/方法名` 生成
56
57
  */
57
58
  template?: string;
58
59
  /**
@@ -63,7 +64,7 @@ export interface TemplateOption {
63
64
  failTemplate?: string | false;
64
65
  /**
65
66
  * 使用的渲染引擎
66
- * - 对应的是 `TemplateSetupOption.engineName`
67
+ * - 对应的是 `TemplateSetupOption.renderName`
67
68
  */
68
- engine?: string;
69
+ render?: string;
69
70
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zenweb/template",
3
- "version": "5.3.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",
@@ -25,6 +25,7 @@
25
25
  "homepage": "https://zenweb.node.ltd",
26
26
  "devDependencies": {
27
27
  "@types/node": "^16.18.126",
28
+ "@zenweb/controller": "^6.7.0",
28
29
  "@zenweb/inject": "^5.4.0",
29
30
  "@zenweb/template-handlebars": "^4.1.0",
30
31
  "@zenweb/template-nunjucks": "^4.1.0",