Skip to content

Commit 15fb67b

Browse files
authoredFeb 13, 2024··
docs: add missing paramter to meet the explaination (#5293)
"viewOptions" is the missing parameter of "render" and "renderString" functions, according to the explainations. So this is the fixture for it.
1 parent 1a3c3eb commit 15fb67b

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed
 

‎site/docs/advanced/view-plugin.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ The following is a simplified code that can be directly [view source](https://gi
6060
const ejs = require('ejs');
6161

6262
Mmdule.exports = class EjsView {
63-
render(filename, locals) {
63+
render(filename, locals, viewOptions) {
64+
65+
const config = Object.assign({}, this.config, viewOptions, { filename });
66+
6467
return new Promise((resolve, reject) => {
6568
// Asynchronous API call
66-
ejs.renderFile(filename, locals, (err, result) => {
69+
ejs.renderFile(filename, locals, config, (err, result) => {
6770
if (err) {
6871
reject(err);
6972
} else {
@@ -73,10 +76,11 @@ Mmdule.exports = class EjsView {
7376
});
7477
}
7578

76-
renderString(tpl, locals) {
79+
renderString(tpl, locals, viewOptions) {
80+
const config = Object.assign({}, this.config, viewOptions, { cache: null });
7781
try {
7882
// Synchronous API call
79-
return Promise.resolve(ejs.render(tpl, locals));
83+
return Promise.resolve(ejs.render(tpl, locals, config));
8084
} catch (err) {
8185
return Promise.reject(err);
8286
}
@@ -88,15 +92,15 @@ Mmdule.exports = class EjsView {
8892

8993
The three parameters of the `render` method are:
9094

91-
- filename: is the path to the complete file. The framework determines if the file exists when looking for the file. It does not need to be processed here.
92-
- locals: The data needs rendering. It comes from `app.locals`, `ctx.locals` and calls `render` methods. The framework also has built in `ctx`, `request`, `ctx.helper` objects.
93-
- viewOptions: The incoming configuration of the user, which can override the default configuration of the template engine. This can be considered based on the characteristics of the template engine. For example, the cache is enabled by default but a page does not need to be cached.
95+
- `filename`: is the path to the complete file. The framework determines if the file exists when looking for the file. It does not need to be processed here.
96+
- `locals`: The data needs rendering. It comes from `app.locals`, `ctx.locals` and calls `render` methods. The framework also has built in `ctx`, `request`, `ctx.helper` objects.
97+
- `viewOptions`: The incoming configuration of the user, which can override the default configuration of the template engine. This can be considered based on the characteristics of the template engine. For example, the cache is enabled by default but a page does not need to be cached.
9498

9599
The three parameters of the `renderString` method:
96100

97-
- tpl: template string, not file path.
98-
- locals: same with `render`.
99-
- viewOptions: same with `render`.
101+
- `tpl`: template string, not file path.
102+
- `locals`: same with `render`.
103+
- `viewOptions`: same with `render`.
100104

101105
## Plugin Configuration
102106

‎site/docs/advanced/view-plugin.zh-CN.md

+12-11
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ View 基类需要提供 `render` 和 `renderString` 两个方法,支持 genera
5656
```js
5757
const ejs = require('ejs');
5858

59-
class EjsView {
60-
render(filename, locals) {
59+
Mmdule.exports = class EjsView {
60+
render(filename, locals, viewOptions) {
61+
62+
const config = Object.assign({}, this.config, viewOptions, { filename });
63+
6164
return new Promise((resolve, reject) => {
6265
// 异步调用 API
63-
ejs.renderFile(filename, locals, function(err, result) {
66+
ejs.renderFile(filename, locals, config, (err, result) => {
6467
if (err) {
6568
reject(err);
6669
} else {
@@ -70,28 +73,26 @@ class EjsView {
7073
});
7174
}
7275

73-
renderString(tpl, locals) {
76+
renderString(tpl, locals, viewOptions) {
77+
const config = Object.assign({}, this.config, viewOptions, { cache: null });
7478
try {
7579
// 同步调用 API
76-
return Promise.resolve(ejs.render(tpl, locals));
80+
return Promise.resolve(ejs.render(tpl, locals, config));
7781
} catch (err) {
7882
return Promise.reject(err);
7983
}
8084
}
81-
}
82-
83-
module.exports = EjsView;
85+
};
8486
```
8587

8688
### 参数
8789

88-
- `render` 方法的参数:
90+
`render` 方法的参数:
8991
- `filename`:是完整文件路径,框架查找文件时已确认文件是否存在,因此这里不需要处理。
9092
- `locals`:渲染所需数据,来源包括 `app.locals``ctx.locals` 以及调用 `render` 方法传入的数据。框架还内置了 `ctx``request``ctx.helper` 这几个对象。
9193
- `viewOptions`:用户传入的配置,可以覆盖模板引擎的默认配置。这个可根据模板引擎的特征考虑是否支持。例如,默认开启了缓存,而某个页面不需要缓存。
9294

93-
- `renderString` 方法的三个参数
94-
95+
`renderString` 方法的三个参数:
9596
- `tpl`: 模板字符串,没有文件路径。
9697
- `locals`: 同 `render`
9798
- `viewOptions`: 同 `render`

0 commit comments

Comments
 (0)
Please sign in to comment.