@midwayjs/view 3.0.0-beta.9 → 3.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 +220 -4
- package/dist/config/config.default.d.ts +20 -1
- package/dist/config/config.default.js +19 -8
- package/dist/config/config.local.d.ts +3 -2
- package/dist/config/config.local.js +4 -1
- package/dist/configuration.d.ts +2 -1
- package/dist/configuration.js +44 -14
- package/dist/contextView.d.ts +18 -0
- package/dist/contextView.js +94 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/interface.d.ts +25 -0
- package/dist/interface.js +3 -0
- package/dist/viewManager.d.ts +10 -9
- package/dist/viewManager.js +67 -19
- package/index.d.ts +27 -45
- package/package.json +10 -16
- package/CHANGELOG.md +0 -518
package/README.md
CHANGED
|
@@ -1,11 +1,227 @@
|
|
|
1
1
|
# midway-view
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
[](https://github.com/midwayjs/midway/pulls)
|
|
3
|
+
Base view component for midway.
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
@midwayjs/view don't have build-in view engine, So you should choose a template engine like ejs, and install `@midwayjs/view-ejs`.
|
|
8
|
+
View component will be auto install and enable by import `view-ejs`.
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
$ npm i @midwayjs/view-ejs --save
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
First, import component in `src/configuration.ts`.
|
|
17
|
+
|
|
18
|
+
```typescript
|
|
19
|
+
import { Configuration } from '@midwayjs/decorator';
|
|
20
|
+
import * as view from '@midwayjs/view-ejs';
|
|
21
|
+
import { join } from 'path'
|
|
22
|
+
|
|
23
|
+
@Configuration({
|
|
24
|
+
imports: [
|
|
25
|
+
// ...
|
|
26
|
+
view
|
|
27
|
+
],
|
|
28
|
+
importConfigs: [
|
|
29
|
+
join(__dirname, 'config')
|
|
30
|
+
]
|
|
31
|
+
})
|
|
32
|
+
export class ContainerLifeCycle {
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Configure the mapping, the file with `.ejs` extension will be rendered by ejs.
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
// src/config/config.default.ts
|
|
40
|
+
export const view = {
|
|
41
|
+
defaultViewEngine: 'ejs',
|
|
42
|
+
mapping: {
|
|
43
|
+
'.ejs': 'ejs',
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// ejs config
|
|
48
|
+
export const ejs = {};
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
In controller, you can call `ctx.render`.
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { Inject, Provide } from '@midwayjs/decorator';
|
|
55
|
+
import { Context } from '@midwayjs/koa';
|
|
56
|
+
|
|
57
|
+
@Controller('/')
|
|
58
|
+
export class HomeController {
|
|
59
|
+
|
|
60
|
+
@Inject()
|
|
61
|
+
ctx: Context;
|
|
62
|
+
|
|
63
|
+
@Get('/')
|
|
64
|
+
async render(){
|
|
65
|
+
await this.ctx.render('hello.ejs', {
|
|
66
|
+
data: 'world',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Use multiple view engine
|
|
73
|
+
|
|
74
|
+
@midwayjs/view support multiple view engine, so you can use more than one template engine in one application.
|
|
75
|
+
|
|
76
|
+
If you want add another template engine like nunjucks, then you can add @midwayjs/view-nunjucks component.
|
|
77
|
+
|
|
78
|
+
Configure the plugin and mapping
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
// src/config/config.default.ts
|
|
82
|
+
export const view = {
|
|
83
|
+
mapping: {
|
|
84
|
+
'.ejs': 'ejs',
|
|
85
|
+
'.nj': 'nunjucks',
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
```
|
|
89
|
+
You can simply render the file with .nj extension.
|
|
90
|
+
|
|
91
|
+
```typescript
|
|
92
|
+
await this.ctx.render('user.nj');
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## Write a view engine
|
|
96
|
+
|
|
97
|
+
Create a view engine class first, and implement render and renderString, if the template engine don't support, just throw an error.
|
|
98
|
+
|
|
99
|
+
```typescript
|
|
100
|
+
// lib/view.ts
|
|
101
|
+
import { Provide } from '@midwayjs/decorator';
|
|
102
|
+
|
|
103
|
+
@Provide()
|
|
104
|
+
export class MyView {
|
|
105
|
+
|
|
106
|
+
@Config('xxxx')
|
|
107
|
+
viewConfig;
|
|
108
|
+
|
|
109
|
+
async render(fullpath, locals) {
|
|
110
|
+
return myengine.render(fullpath, locals);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async renderString() { throw new Error('not implement'); }
|
|
114
|
+
};
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
These methods receive three arguments, `renderString` will pass tpl as the first argument instead of name in `render`.
|
|
118
|
+
|
|
119
|
+
`render(name, locals, viewOptions)`
|
|
120
|
+
|
|
121
|
+
- name: the file path that can resolve from root (`/view` by default)
|
|
122
|
+
- locals: data used by template
|
|
123
|
+
- viewOptions: the view options for each render, it can override the view default config in `config/config.default.js`. Plugin should implement it if it has config.
|
|
124
|
+
When you implement view engine, you will receive this options from `render`, the options contain:
|
|
125
|
+
- root: it will resolve the name to full path, but seperating root and name in viewOptions.
|
|
126
|
+
- name: the original name when call render
|
|
127
|
+
- locals: the original locals when call render
|
|
128
|
+
|
|
129
|
+
`renderString(tpl, locals, viewOptions)`
|
|
130
|
+
|
|
131
|
+
- tpl: the template string instead of the file, using in `renderString`
|
|
132
|
+
- locals: same as `render`
|
|
133
|
+
- viewOptions: same as `render`
|
|
134
|
+
|
|
135
|
+
### Register
|
|
136
|
+
|
|
137
|
+
After define a view engine, you can register it.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
// src/configuration.ts
|
|
141
|
+
import { Configuration, Inject, Provide } from '@midwayjs/decorator';
|
|
142
|
+
import * as koa from '@midwayjs/koa';
|
|
143
|
+
import * as view from '@midwayjs/view';
|
|
144
|
+
import { MyView } from './lib/my';
|
|
145
|
+
|
|
146
|
+
@Configuration({
|
|
147
|
+
imports: [koa, view],
|
|
148
|
+
importConfigs: [join(__dirname, 'config')]
|
|
149
|
+
})
|
|
150
|
+
export class AutoConfiguration {
|
|
151
|
+
|
|
152
|
+
@Inject()
|
|
153
|
+
viewManager: view.ViewManager;
|
|
154
|
+
|
|
155
|
+
async onReady(){
|
|
156
|
+
this.viewManager.use('ejs', MyView);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
You can define a view engine name, normally it's a template name.
|
|
163
|
+
|
|
164
|
+
## Configuration
|
|
165
|
+
|
|
166
|
+
### Root
|
|
167
|
+
|
|
168
|
+
Root is `${appDir}/view` by default, but you can define multiple directory.
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
export default appInfo => {
|
|
172
|
+
const appDir = appInfo.appDir;
|
|
173
|
+
return {
|
|
174
|
+
view: {
|
|
175
|
+
rootDir: {
|
|
176
|
+
default: `${appDir}/view`,
|
|
177
|
+
anotherDir: `${appDir}/view2`
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### defaultExtension
|
|
185
|
+
|
|
186
|
+
When render a file, you should specify a extension that let @midway/view know which engine you want to use. However you can define `defaultExtension` without write the extension.
|
|
187
|
+
|
|
188
|
+
```typescript
|
|
189
|
+
// src/config/config.default.ts
|
|
190
|
+
export const view = {
|
|
191
|
+
defaultExtension: '.html',
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// controller
|
|
195
|
+
import { Inject, Provide } from '@midwayjs/decorator';
|
|
196
|
+
import { Context } from '@midwayjs/koa';
|
|
197
|
+
|
|
198
|
+
@Controller('/')
|
|
199
|
+
export class HomeController {
|
|
200
|
+
|
|
201
|
+
@Inject()
|
|
202
|
+
ctx: Context;
|
|
203
|
+
|
|
204
|
+
@Get('/')
|
|
205
|
+
async render(){
|
|
206
|
+
// render user.html
|
|
207
|
+
await this.ctx.render('user');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### viewEngine and defaultViewEngine
|
|
213
|
+
|
|
214
|
+
If you are using `renderString`, you should specify viewEngine in view config, see example above.
|
|
215
|
+
|
|
216
|
+
However, you can define `defaultViewEngine` without set each time.
|
|
217
|
+
|
|
218
|
+
```js
|
|
219
|
+
// config/config.default.js
|
|
220
|
+
export const view = {
|
|
221
|
+
defaultViewEngine: 'ejs',
|
|
222
|
+
};
|
|
223
|
+
```
|
|
7
224
|
|
|
8
|
-
Document: [https://midwayjs.org](https://midwayjs.org)
|
|
9
225
|
|
|
10
226
|
## License
|
|
11
227
|
|
|
@@ -1,3 +1,22 @@
|
|
|
1
|
-
declare const _default: (appInfo: any) =>
|
|
1
|
+
declare const _default: (appInfo: any) => {
|
|
2
|
+
/**
|
|
3
|
+
* view default config
|
|
4
|
+
* @member Config#view
|
|
5
|
+
* @property {String} [rootDir=${appDir}/view] - give a path to find the file, object.values() got array and find for view file
|
|
6
|
+
* @property {Boolean} [cache=true] - whether cache the file's path
|
|
7
|
+
* @property {String} [defaultExtension] - defaultExtension can be added automatically when there is no extension when call `ctx.render`
|
|
8
|
+
* @property {String} [defaultViewEngine] - set the default view engine if you don't want specify the viewEngine every request.
|
|
9
|
+
* @property {Object} mapping - map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
10
|
+
*/
|
|
11
|
+
view: {
|
|
12
|
+
cache: boolean;
|
|
13
|
+
defaultExtension: string;
|
|
14
|
+
defaultViewEngine: string;
|
|
15
|
+
mapping: {};
|
|
16
|
+
rootDir: {
|
|
17
|
+
default: string;
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
};
|
|
2
21
|
export default _default;
|
|
3
22
|
//# sourceMappingURL=config.default.d.ts.map
|
|
@@ -1,13 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const path_1 = require("path");
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
exports.default = appInfo => ({
|
|
5
|
+
/**
|
|
6
|
+
* view default config
|
|
7
|
+
* @member Config#view
|
|
8
|
+
* @property {String} [rootDir=${appDir}/view] - give a path to find the file, object.values() got array and find for view file
|
|
9
|
+
* @property {Boolean} [cache=true] - whether cache the file's path
|
|
10
|
+
* @property {String} [defaultExtension] - defaultExtension can be added automatically when there is no extension when call `ctx.render`
|
|
11
|
+
* @property {String} [defaultViewEngine] - set the default view engine if you don't want specify the viewEngine every request.
|
|
12
|
+
* @property {Object} mapping - map the file extension to view engine, such as `{ '.ejs': 'ejs' }`
|
|
13
|
+
*/
|
|
14
|
+
view: {
|
|
15
|
+
cache: true,
|
|
16
|
+
defaultExtension: '.html',
|
|
17
|
+
defaultViewEngine: '',
|
|
18
|
+
mapping: {},
|
|
19
|
+
rootDir: {
|
|
20
|
+
default: (0, path_1.join)(appInfo.appDir, 'view'),
|
|
10
21
|
},
|
|
11
|
-
}
|
|
12
|
-
};
|
|
22
|
+
},
|
|
23
|
+
});
|
|
13
24
|
//# sourceMappingURL=config.default.js.map
|
package/dist/configuration.d.ts
CHANGED
package/dist/configuration.js
CHANGED
|
@@ -11,28 +11,58 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.ViewConfiguration = void 0;
|
|
13
13
|
const decorator_1 = require("@midwayjs/decorator");
|
|
14
|
-
const mw_util_1 = require("@midwayjs/mw-util");
|
|
15
14
|
const DefaultConfig = require("./config/config.default");
|
|
16
15
|
const LocalConfig = require("./config/config.local");
|
|
17
16
|
const viewManager_1 = require("./viewManager");
|
|
17
|
+
const core_1 = require("@midwayjs/core");
|
|
18
|
+
const contextView_1 = require("./contextView");
|
|
18
19
|
let ViewConfiguration = class ViewConfiguration {
|
|
19
20
|
async onReady(container) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
21
|
+
this.applicationManager
|
|
22
|
+
.getApplications(['koa', 'egg', 'faas'])
|
|
23
|
+
.forEach((app) => {
|
|
24
|
+
Object.defineProperties(app.context, {
|
|
25
|
+
/**
|
|
26
|
+
* Render a file, then set to body, the parameter is same as {@link @ContextView#render}
|
|
27
|
+
* @return {Promise} result
|
|
28
|
+
*/
|
|
29
|
+
render: {
|
|
30
|
+
value: async function (...args) {
|
|
31
|
+
const contextView = await this.requestContext.getAsync(contextView_1.ContextView);
|
|
32
|
+
return contextView.render(...args).then(body => {
|
|
33
|
+
this.body = body;
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
/**
|
|
38
|
+
* Render a file, same as {@link @ContextView#render}
|
|
39
|
+
* @return {Promise} result
|
|
40
|
+
*/
|
|
41
|
+
renderView: {
|
|
42
|
+
value: async function (...args) {
|
|
43
|
+
const contextView = await this.requestContext.getAsync(contextView_1.ContextView);
|
|
44
|
+
return contextView.render(...args);
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
/**
|
|
48
|
+
* Render template string, same as {@link @ContextView#renderString}
|
|
49
|
+
* @return {Promise} result
|
|
50
|
+
*/
|
|
51
|
+
renderString: {
|
|
52
|
+
value: async function (...args) {
|
|
53
|
+
const contextView = await this.requestContext.getAsync(contextView_1.ContextView);
|
|
54
|
+
return contextView.renderString(...args);
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
await container.getAsync(viewManager_1.ViewManager);
|
|
30
60
|
}
|
|
31
61
|
};
|
|
32
62
|
__decorate([
|
|
33
|
-
(0, decorator_1.
|
|
34
|
-
__metadata("design:type",
|
|
35
|
-
], ViewConfiguration.prototype, "
|
|
63
|
+
(0, decorator_1.Inject)(),
|
|
64
|
+
__metadata("design:type", core_1.MidwayApplicationManager)
|
|
65
|
+
], ViewConfiguration.prototype, "applicationManager", void 0);
|
|
36
66
|
ViewConfiguration = __decorate([
|
|
37
67
|
(0, decorator_1.Configuration)({
|
|
38
68
|
namespace: 'view',
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ViewManager } from './viewManager';
|
|
2
|
+
import { IViewEngine, RenderOptions } from './interface';
|
|
3
|
+
/**
|
|
4
|
+
* View instance for each request.
|
|
5
|
+
*
|
|
6
|
+
* It will find the view engine, and render it.
|
|
7
|
+
* The view engine should be registered in {@link ViewManager}.
|
|
8
|
+
*/
|
|
9
|
+
export declare class ContextView implements IViewEngine {
|
|
10
|
+
viewManager: ViewManager;
|
|
11
|
+
viewConfig: any;
|
|
12
|
+
ctx: any;
|
|
13
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
14
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
15
|
+
private getViewEngine;
|
|
16
|
+
private setLocals;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=contextView.d.ts.map
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.ContextView = void 0;
|
|
13
|
+
const viewManager_1 = require("./viewManager");
|
|
14
|
+
const decorator_1 = require("@midwayjs/decorator");
|
|
15
|
+
const assert = require("assert");
|
|
16
|
+
const path_1 = require("path");
|
|
17
|
+
/**
|
|
18
|
+
* View instance for each request.
|
|
19
|
+
*
|
|
20
|
+
* It will find the view engine, and render it.
|
|
21
|
+
* The view engine should be registered in {@link ViewManager}.
|
|
22
|
+
*/
|
|
23
|
+
let ContextView = class ContextView {
|
|
24
|
+
async render(name, locals, options) {
|
|
25
|
+
// retrieve fullpath matching name from `config.root`
|
|
26
|
+
const filename = await this.viewManager.resolve(name);
|
|
27
|
+
options = options !== null && options !== void 0 ? options : {};
|
|
28
|
+
options.name = name;
|
|
29
|
+
options.root = filename.replace((0, path_1.normalize)(name), '').replace(/[/\\]$/, '');
|
|
30
|
+
options.locals = locals;
|
|
31
|
+
// get the name of view engine,
|
|
32
|
+
// if viewEngine is specified in options, don't match extension
|
|
33
|
+
let viewEngineName = options.viewEngine;
|
|
34
|
+
if (!viewEngineName) {
|
|
35
|
+
const ext = (0, path_1.extname)(filename);
|
|
36
|
+
viewEngineName = this.viewManager.extMap.get(ext);
|
|
37
|
+
}
|
|
38
|
+
// use the default view engine that is configured if no matching above
|
|
39
|
+
if (!viewEngineName) {
|
|
40
|
+
viewEngineName = this.viewConfig.defaultViewEngine;
|
|
41
|
+
}
|
|
42
|
+
assert(viewEngineName, `Can't find viewEngine for ${filename}`);
|
|
43
|
+
// get view engine and render
|
|
44
|
+
const view = await this.getViewEngine(viewEngineName);
|
|
45
|
+
return await view.render(filename, this.setLocals(locals), options);
|
|
46
|
+
}
|
|
47
|
+
async renderString(tpl, locals, options) {
|
|
48
|
+
var _a;
|
|
49
|
+
options = options !== null && options !== void 0 ? options : {};
|
|
50
|
+
const viewEngineName = (_a = options.viewEngine) !== null && _a !== void 0 ? _a : this.viewConfig.defaultViewEngine;
|
|
51
|
+
assert(viewEngineName, "Can't find viewEngine");
|
|
52
|
+
// get view engine and render
|
|
53
|
+
const view = await this.getViewEngine(viewEngineName);
|
|
54
|
+
return await view.renderString(tpl, this.setLocals(locals), options);
|
|
55
|
+
}
|
|
56
|
+
async getViewEngine(name) {
|
|
57
|
+
// get view engine
|
|
58
|
+
const ViewEngine = this.viewManager.get(name);
|
|
59
|
+
assert(ViewEngine, `Can't find ViewEngine "${name}"`);
|
|
60
|
+
// use view engine to render
|
|
61
|
+
const engine = await this.ctx.requestContext.getAsync(ViewEngine);
|
|
62
|
+
// wrap render and renderString to support both async function and generator function
|
|
63
|
+
if (engine.render) {
|
|
64
|
+
engine.render = decorator_1.Utils.toAsyncFunction(engine.render);
|
|
65
|
+
}
|
|
66
|
+
if (engine.renderString) {
|
|
67
|
+
engine.renderString = decorator_1.Utils.toAsyncFunction(engine.renderString);
|
|
68
|
+
}
|
|
69
|
+
return engine;
|
|
70
|
+
}
|
|
71
|
+
setLocals(locals) {
|
|
72
|
+
return Object.assign({
|
|
73
|
+
ctx: this.ctx,
|
|
74
|
+
request: this.ctx.request,
|
|
75
|
+
}, this.ctx.locals, locals);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
__decorate([
|
|
79
|
+
(0, decorator_1.Inject)(),
|
|
80
|
+
__metadata("design:type", viewManager_1.ViewManager)
|
|
81
|
+
], ContextView.prototype, "viewManager", void 0);
|
|
82
|
+
__decorate([
|
|
83
|
+
(0, decorator_1.Config)('view'),
|
|
84
|
+
__metadata("design:type", Object)
|
|
85
|
+
], ContextView.prototype, "viewConfig", void 0);
|
|
86
|
+
__decorate([
|
|
87
|
+
(0, decorator_1.Inject)(),
|
|
88
|
+
__metadata("design:type", Object)
|
|
89
|
+
], ContextView.prototype, "ctx", void 0);
|
|
90
|
+
ContextView = __decorate([
|
|
91
|
+
(0, decorator_1.Provide)()
|
|
92
|
+
], ContextView);
|
|
93
|
+
exports.ContextView = ContextView;
|
|
94
|
+
//# sourceMappingURL=contextView.js.map
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -13,5 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
13
13
|
exports.Configuration = void 0;
|
|
14
14
|
var configuration_1 = require("./configuration");
|
|
15
15
|
Object.defineProperty(exports, "Configuration", { enumerable: true, get: function () { return configuration_1.ViewConfiguration; } });
|
|
16
|
+
__exportStar(require("./interface"), exports);
|
|
17
|
+
__exportStar(require("./contextView"), exports);
|
|
16
18
|
__exportStar(require("./viewManager"), exports);
|
|
17
19
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export interface RenderOptions {
|
|
2
|
+
name?: string;
|
|
3
|
+
root?: string;
|
|
4
|
+
locals?: Record<string, any>;
|
|
5
|
+
viewEngine?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface IViewEngine {
|
|
8
|
+
/**
|
|
9
|
+
* Render a file by view engine, then set to body
|
|
10
|
+
* @param {String} name - the file path based on root
|
|
11
|
+
* @param {Object} [locals] - data used by template
|
|
12
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
13
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
14
|
+
*/
|
|
15
|
+
render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Render a template string by view engine
|
|
18
|
+
* @param {String} tpl - template string
|
|
19
|
+
* @param {Object} [locals] - data used by template
|
|
20
|
+
* @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine
|
|
21
|
+
* @return {Promise<String>} result - return a promise with a render result
|
|
22
|
+
*/
|
|
23
|
+
renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=interface.d.ts.map
|
package/dist/viewManager.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { IViewEngine } from './interface';
|
|
2
|
+
export declare class ViewManager extends Map {
|
|
2
3
|
app: any;
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
viewConfig: any;
|
|
5
|
+
config: any;
|
|
6
|
+
extMap: Map<any, any>;
|
|
7
|
+
fileMap: Map<any, any>;
|
|
8
|
+
init(): void;
|
|
7
9
|
/**
|
|
8
10
|
* This method can register view engine.
|
|
9
11
|
*
|
|
@@ -18,15 +20,14 @@ export declare class ViewManager {
|
|
|
18
20
|
* @param {String} name - the name of view engine
|
|
19
21
|
* @param {Object} viewEngine - the class of view engine
|
|
20
22
|
*/
|
|
21
|
-
use(name: string, viewEngine: any): void;
|
|
23
|
+
use(name: string, viewEngine: new (...args: any[]) => IViewEngine): void;
|
|
22
24
|
/**
|
|
23
25
|
* Resolve the path based on the given name,
|
|
24
|
-
* if the name is `user.html` and root is `
|
|
25
|
-
* it will return `
|
|
26
|
+
* if the name is `user.html` and root is `view` (by default),
|
|
27
|
+
* it will return `view/user.html`
|
|
26
28
|
* @param {String} name - the given path name, it's relative to config.root
|
|
27
29
|
* @return {String} filename - the full path
|
|
28
30
|
*/
|
|
29
31
|
resolve(name: string): Promise<string>;
|
|
30
|
-
get(key: any): any;
|
|
31
32
|
}
|
|
32
33
|
//# sourceMappingURL=viewManager.d.ts.map
|