@zenweb/template 3.0.3 → 3.2.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/CHANGELOG.md +6 -0
- package/README.md +11 -19
- package/dist/render.js +9 -2
- package/dist/types.d.ts +10 -1
- package/package.json +5 -17
- package/dist/engine/handlebars.d.ts +0 -15
- package/dist/engine/handlebars.js +0 -36
- package/dist/engine/nunjucks.d.ts +0 -36
- package/dist/engine/nunjucks.js +0 -34
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -5,20 +5,19 @@ zenweb template render module
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
|
|
8
|
+
yarn add @zenweb/template
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
### nunjucks
|
|
12
12
|
|
|
13
13
|
```bash
|
|
14
|
-
|
|
15
|
-
npm install @types/nunjucks --save-dev
|
|
14
|
+
yarn add @zenweb/template-nunjucks
|
|
16
15
|
```
|
|
17
16
|
|
|
18
17
|
### handlebars
|
|
19
18
|
|
|
20
19
|
```bash
|
|
21
|
-
|
|
20
|
+
yarn add @zenweb/template-handlebars
|
|
22
21
|
```
|
|
23
22
|
|
|
24
23
|
## App Config
|
|
@@ -27,36 +26,29 @@ src/index.ts
|
|
|
27
26
|
```ts
|
|
28
27
|
import { create } from 'zenweb';
|
|
29
28
|
import modTemplate from '@zenweb/template';
|
|
30
|
-
import nunjucks from '@zenweb/template
|
|
31
|
-
import handlebars from '@zenweb/template
|
|
29
|
+
import nunjucks from '@zenweb/template-nunjucks';
|
|
30
|
+
import handlebars from '@zenweb/template-handlebars';
|
|
32
31
|
|
|
33
|
-
|
|
32
|
+
create()
|
|
34
33
|
|
|
35
|
-
|
|
34
|
+
.setup(modTemplate({
|
|
36
35
|
matchPath: [/\.html$/i],
|
|
37
36
|
engine: nunjucks({
|
|
38
37
|
filter: require('./template/filter'),
|
|
39
38
|
global: require('./template/global'),
|
|
40
39
|
}),
|
|
41
|
-
}))
|
|
40
|
+
}))
|
|
42
41
|
|
|
43
42
|
// Multiple Engines
|
|
44
|
-
|
|
43
|
+
.setup(modTemplate({
|
|
45
44
|
type: 'xml',
|
|
46
45
|
matchPath: [/\.xml$/i],
|
|
47
46
|
templateAffix: '.xml',
|
|
48
47
|
engineName: 'xml',
|
|
49
48
|
engine: handlebars(),
|
|
50
|
-
}))
|
|
49
|
+
}))
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
tsconfig.json
|
|
56
|
-
```json
|
|
57
|
-
"compilerOptions": {
|
|
58
|
-
"moduleResolution": "node16",
|
|
59
|
-
}
|
|
51
|
+
.start();
|
|
60
52
|
```
|
|
61
53
|
|
|
62
54
|
## Use
|
package/dist/render.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TemplateRender = exports.TEMPLATE_OPTION = void 0;
|
|
4
|
+
const result_1 = require("@zenweb/result");
|
|
4
5
|
const utils_1 = require("./utils");
|
|
5
6
|
exports.TEMPLATE_OPTION = Symbol('template@option');
|
|
6
7
|
class TemplateRender {
|
|
@@ -35,8 +36,14 @@ class TemplateRender {
|
|
|
35
36
|
return false;
|
|
36
37
|
}
|
|
37
38
|
render(ctx, data) {
|
|
38
|
-
const opt = ctx[exports.TEMPLATE_OPTION];
|
|
39
|
-
let template = opt
|
|
39
|
+
const opt = ctx[exports.TEMPLATE_OPTION] || {};
|
|
40
|
+
let template = opt.template;
|
|
41
|
+
if (data instanceof result_1.ResultFail) {
|
|
42
|
+
data = { fail: data };
|
|
43
|
+
if (opt.failTemplate !== false) {
|
|
44
|
+
template = opt.failTemplate || this._option.failTemplate;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
40
47
|
if (!template) {
|
|
41
48
|
template = ctx.path.slice(1);
|
|
42
49
|
if (this._option.templateAffix) {
|
package/dist/types.d.ts
CHANGED
|
@@ -24,7 +24,10 @@ export interface TemplateSetupOption {
|
|
|
24
24
|
*/
|
|
25
25
|
templateAffix?: string;
|
|
26
26
|
/**
|
|
27
|
-
*
|
|
27
|
+
* 使用 `ctx.fail` 操作所使用的模版 - 全局
|
|
28
|
+
* - 不指定则使用 `TemplateOption.template` 值
|
|
29
|
+
* - 指定后可以在控制器中设置 `TemplateOption.failTemplate = false` 关闭全局 `failTemplate`
|
|
30
|
+
* @default undefined
|
|
28
31
|
*/
|
|
29
32
|
failTemplate?: string;
|
|
30
33
|
/**
|
|
@@ -42,6 +45,12 @@ export interface TemplateOption {
|
|
|
42
45
|
* 指定模版名,不指定取 `ctx.path`
|
|
43
46
|
*/
|
|
44
47
|
template?: string;
|
|
48
|
+
/**
|
|
49
|
+
* 使用 `ctx.fail` 操作所使用的模版
|
|
50
|
+
* - 默认使用全局配置的 `failTemplate`
|
|
51
|
+
* - 如果设置为 `false` 则强制使用 `template`
|
|
52
|
+
*/
|
|
53
|
+
failTemplate?: string | false;
|
|
45
54
|
/**
|
|
46
55
|
* 使用的渲染引擎
|
|
47
56
|
* - 对应的是 `TemplateSetupOption.engineName`
|
package/package.json
CHANGED
|
@@ -1,18 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zenweb/template",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.2.0",
|
|
4
4
|
"description": "zenweb template render module",
|
|
5
|
-
"exports":
|
|
6
|
-
".": "./dist/index.js",
|
|
7
|
-
"./handlebars": {
|
|
8
|
-
"require": "./dist/engine/handlebars.js",
|
|
9
|
-
"types": "./dist/engine/handlebars.d.ts"
|
|
10
|
-
},
|
|
11
|
-
"./nunjucks": {
|
|
12
|
-
"require": "./dist/engine/nunjucks.js",
|
|
13
|
-
"types": "./dist/engine/nunjucks.d.ts"
|
|
14
|
-
}
|
|
15
|
-
},
|
|
5
|
+
"exports": "./dist/index.js",
|
|
16
6
|
"types": "./dist/index.d.ts",
|
|
17
7
|
"scripts": {
|
|
18
8
|
"build": "rimraf dist && tsc",
|
|
@@ -29,16 +19,14 @@
|
|
|
29
19
|
"keywords": [
|
|
30
20
|
"zenweb",
|
|
31
21
|
"koa",
|
|
32
|
-
"template"
|
|
33
|
-
"nunjucks"
|
|
22
|
+
"template"
|
|
34
23
|
],
|
|
35
24
|
"license": "MIT",
|
|
36
25
|
"homepage": "https://zenweb.node.ltd",
|
|
37
26
|
"devDependencies": {
|
|
38
|
-
"@
|
|
27
|
+
"@zenweb/template-handlebars": "^3.0.0",
|
|
28
|
+
"@zenweb/template-nunjucks": "^3.0.0",
|
|
39
29
|
"cross-env": "^7.0.3",
|
|
40
|
-
"handlebars": "^4.7.7",
|
|
41
|
-
"nunjucks": "^3.2.3",
|
|
42
30
|
"rimraf": "^4.4.1",
|
|
43
31
|
"ts-node": "^10.9.1",
|
|
44
32
|
"typescript": "^5.0.2",
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/// <reference types="handlebars" />
|
|
2
|
-
import { TemplateEngine } from '../types';
|
|
3
|
-
export interface HandlebarsOption extends CompileOptions {
|
|
4
|
-
/**
|
|
5
|
-
* 模版文件夹
|
|
6
|
-
* @default './template'
|
|
7
|
-
*/
|
|
8
|
-
path?: string | string[];
|
|
9
|
-
/**
|
|
10
|
-
* 缓存已编译模版
|
|
11
|
-
* @default true
|
|
12
|
-
*/
|
|
13
|
-
cache?: boolean;
|
|
14
|
-
}
|
|
15
|
-
export default function (option?: HandlebarsOption): TemplateEngine;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/// <reference types="handlebars" />
|
|
4
|
-
const path = require("path");
|
|
5
|
-
const fs = require("fs");
|
|
6
|
-
const util_1 = require("util");
|
|
7
|
-
const handlebars_1 = require("handlebars");
|
|
8
|
-
const utils_1 = require("../utils");
|
|
9
|
-
const fs_exists = (0, util_1.promisify)(fs.exists);
|
|
10
|
-
const fs_readFile = (0, util_1.promisify)(fs.readFile);
|
|
11
|
-
function default_1(option) {
|
|
12
|
-
const opt = Object.assign({
|
|
13
|
-
path: './template',
|
|
14
|
-
cache: true,
|
|
15
|
-
}, option);
|
|
16
|
-
(0, utils_1.debug)('option: %o', opt);
|
|
17
|
-
const paths = (Array.isArray(opt.path) ? opt.path : [opt.path]).map(p => (0, utils_1.cwdPath)(p));
|
|
18
|
-
async function loadTemplate(template) {
|
|
19
|
-
for (const p of paths) {
|
|
20
|
-
const filename = path.join(p, template);
|
|
21
|
-
if (await fs_exists(filename)) {
|
|
22
|
-
return await fs_readFile(filename, 'utf-8');
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
throw new Error(`Template file not found: ${template}`);
|
|
26
|
-
}
|
|
27
|
-
const cache = {};
|
|
28
|
-
return async function handlebars(template, data) {
|
|
29
|
-
if (opt.cache && !cache[template]) {
|
|
30
|
-
const content = await loadTemplate(template);
|
|
31
|
-
cache[template] = (0, handlebars_1.compile)(content, opt);
|
|
32
|
-
}
|
|
33
|
-
return cache[template](data);
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
exports.default = default_1;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Environment, ConfigureOptions, Extension } from 'nunjucks';
|
|
2
|
-
import { TemplateEngine } from '../types';
|
|
3
|
-
export interface NunjucksOption {
|
|
4
|
-
/**
|
|
5
|
-
* 模版文件夹
|
|
6
|
-
* @default './template'
|
|
7
|
-
*/
|
|
8
|
-
path?: string | string[];
|
|
9
|
-
/**
|
|
10
|
-
* 配置过滤器
|
|
11
|
-
*/
|
|
12
|
-
filter?: {
|
|
13
|
-
[name: string]: (...args: any[]) => any;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* 配置自定义扩展
|
|
17
|
-
*/
|
|
18
|
-
extension?: {
|
|
19
|
-
[name: string]: Extension;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* 配置全局变量
|
|
23
|
-
*/
|
|
24
|
-
global?: {
|
|
25
|
-
[name: string]: any;
|
|
26
|
-
};
|
|
27
|
-
/**
|
|
28
|
-
* 设置 nunjucks 配置
|
|
29
|
-
*/
|
|
30
|
-
nunjucksConfig?: ConfigureOptions;
|
|
31
|
-
/**
|
|
32
|
-
* 设置 nunjucks 环境变量
|
|
33
|
-
*/
|
|
34
|
-
configureEnvironment?: (env: Environment) => any;
|
|
35
|
-
}
|
|
36
|
-
export default function (option?: NunjucksOption): TemplateEngine;
|
package/dist/engine/nunjucks.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const nunjucks_1 = require("nunjucks");
|
|
4
|
-
const utils_1 = require("../utils");
|
|
5
|
-
function default_1(option) {
|
|
6
|
-
const opt = Object.assign({
|
|
7
|
-
path: './template',
|
|
8
|
-
}, option);
|
|
9
|
-
(0, utils_1.debug)('option: %o', opt);
|
|
10
|
-
const paths = (Array.isArray(opt.path) ? opt.path : [opt.path]).map(p => (0, utils_1.cwdPath)(p));
|
|
11
|
-
const env = (0, nunjucks_1.configure)(paths, opt.nunjucksConfig);
|
|
12
|
-
if (opt.filter) {
|
|
13
|
-
Object.entries(opt.filter).forEach(([name, func]) => env.addFilter(name, func));
|
|
14
|
-
}
|
|
15
|
-
if (opt.extension) {
|
|
16
|
-
Object.entries(opt.extension).forEach(([name, ext]) => env.addExtension(name, ext));
|
|
17
|
-
}
|
|
18
|
-
if (opt.global) {
|
|
19
|
-
Object.entries(opt.global).forEach(([name, value]) => env.addGlobal(name, value));
|
|
20
|
-
}
|
|
21
|
-
if (typeof opt.configureEnvironment === 'function') {
|
|
22
|
-
opt.configureEnvironment(env);
|
|
23
|
-
}
|
|
24
|
-
return function nunjucks(name, data) {
|
|
25
|
-
return new Promise((resolve, reject) => {
|
|
26
|
-
env.render(name, data, (err, res) => {
|
|
27
|
-
if (err)
|
|
28
|
-
return reject(err);
|
|
29
|
-
resolve(res);
|
|
30
|
-
});
|
|
31
|
-
});
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
exports.default = default_1;
|