-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: view #228
docs: view #228
Conversation
|
||
绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。 | ||
|
||
而 egg 并不强制你使用某种模板引擎,开发者可以依据下文的规范, 来封装对应的模板引擎插件。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
egg => 框架
你 => 我们
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
而 egg 并不强制你使用某种模板引擎,开发者可以依据下文的规范, 来封装对应的模板引擎插件。
而框架并不强制使用某种模板引擎,我们可以依据下文的规范, 来封装对应的模板引擎插件。
858fc9e
to
0683835
Compare
app.locals = { a: 2 }; | ||
console.log(this.locals); | ||
|
||
// assign directly is ok, will merge by setter, output: { a: 1, b: 1, c: 1, d: 4 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c: 3
c20bb20
to
6390e55
Compare
|
||
绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。 | ||
|
||
框架并不强制你使用某种模板引擎,并且没有内置的 view 插件,只是约定了 [view 插件开发规范](../practice/view.md),开发者需根据团队技术选型自行引入。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
view 插件开发规范
这个 view 大写首字母还是小写好?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果分开,开发规范就不适合放到 practice 里了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个路径错了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
放到 advanced ?
69c7167
to
781e210
Compare
// config/config.default.js | ||
exports.view = { | ||
dir: path.join(appInfo.baseDir, 'app/view'), | ||
// should set to `false` at local development mode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
插件自己配置了 local 环境下为 false 吧?可以说明一下
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个有约定?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
之前的 web.md 没有, 这次写文档的时候, 想加上这些实践规约, 统一命名好一点吧?
在渲染页面的过程中,我们通常需要一个变量来收集需要传递给模板的变量,在框架里面,我们提供了 `app.locals` 和 `this.locals`。 | ||
|
||
- `app.locals` 为全局的,一般在 `app.js` 里面配置全局变量。 | ||
- `this.locals` 为单次请求的,会合并 `app.locals` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
中间行没有句号
{ | ||
"name": "egg-view-nunjucks", | ||
"eggPlugin": { | ||
"name": "view" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
name 换成真实的吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是真实的,不同 view 插件都要用 view
|
||
绝大多数情况,我们都需要读取数据后渲染模板,然后呈现给用户。故我们需要引入对应的模板引擎。 | ||
|
||
框架并不强制你使用某种模板引擎,并且没有内置的 view 插件,只是约定了 [view 插件开发规范](../practice/view.md),开发者需根据团队技术选型自行引入。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果分开,开发规范就不适合放到 practice 里了
|
||
1. 引入 view 插件 | ||
|
||
```bash |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
list 都需要缩进
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像看到其他的都没有缩进, 渲染后会自动缩进的吧.
这里缩进后, 下面的代码块缩进似乎不好看
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不会自动的。
exports.view = { | ||
enable: true, | ||
package: 'egg-view-nunjucks', | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
分号
// config/config.default.js | ||
exports.view = { | ||
dir: path.join(appInfo.baseDir, 'app/view'), | ||
// should set to `false` at local development mode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个有约定?
```js | ||
// config/config.default.js | ||
exports.view = { | ||
dir: path.join(appInfo.baseDir, 'app/view'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个有约定?
appInfo 在哪
// mount `View` class to app | ||
// egg will create an instance to `ctx.view` at every request | ||
// you can use `this.render` at controller | ||
get [Symbol.for('egg#view')]() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里也要说明下,英文都改了
const path = require('path'); | ||
|
||
module.exports = appInfo => { | ||
const config = {}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要不我们改成直接 return 的?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module.exports = appInfo => {
return {
/**
* View options
* @member Config#view
* @property {String} dir - full path of template dir, support multiple path by using comma, defaults to `{app_root}/app/view`.
* @property {Boolean} cache - whether cache template, default to true except false at local env.
*/
view: {
dir: path.join(appInfo.baseDir, 'app/view'),
cache: true,
}
}
};
这样? 貌似不是很好看
|
||
### filter | ||
|
||
模板引擎中最常用的就是 filter,故我们在框架的[扩展](../basics/extend.md)之上增加对 `app/extend/filter.js` 的支持: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
filter 可以弱化,我们强调 helper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
也要介绍到这个扩展吧? helper 比起 filter 有什么优点?
// {plugin_root}/app/extend/application.js | ||
const helper = require('../../lib/helper'); | ||
module.exports = { | ||
get ViewHelper() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里不用挂 app 了?直接 require?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
也行, 之前是从 chair 那里拷贝的, ViewHelper
挂出来也没啥用
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helper 这个类还是需要缓存起来的,不在 app 就得在 View 里面。
@@ -0,0 +1,259 @@ | |||
title: View 插件开发规范 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
放到 advanced 好了
Current coverage is 97.89% (diff: 100%)@@ master #228 diff @@
==========================================
Files 34 34
Lines 902 902
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
Hits 883 883
Misses 19 19
Partials 0 0
|
const helper = require('./helper'); | ||
|
||
module.exports = class MyCustomView { | ||
get Helper() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@popomore 不在 app 定义 ViewHelper, 就得在这里缓存下. 你看哪种方式好点?
可以 review 了. |
9da8f48
to
cc15897
Compare
}); | ||
``` | ||
|
||
挂载到 application : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
扩展到 application
render(name, locals) { | ||
// name 为模板文件的相对于 `app/view` 的路径 | ||
return new Promise((resolve, reject) => { | ||
this.app.viewEngine.render(name, locals, function(err, result) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
function(err, result) {
=> (err, result) => {
|
||
配置的 key 为 `view`,并提供至少两个配置属性: | ||
|
||
- {String} dir - 模板文件的根目录,绝对路径。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
要说一下通过逗号分隔支持多路径的形式
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在下面的代码示例里面增加了
$ npm i egg-view-nunjucks --save | ||
``` | ||
|
||
2. 启用插件 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
奇怪, 在 vscode 的 markdown preview 里面是正常的
module.exports = function* home(){ | ||
const data = { name: 'egg' }; | ||
|
||
// render a template, path releate to `app/view` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
relate
没啥大问题。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
648ffab
to
75b36e8
Compare
OK,都更新了,我这边有 2 个待确定的点, @popomore 看看:
|
@atian25 看了下确实原来的方式好,辛苦了 |
} | ||
// ... | ||
} | ||
return ViewHelper; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里直接设 app.ViewHelper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
现在那样可以延迟初始化
|
||
get helper() { | ||
if (!this[HELPER]) { | ||
this[HELPER] = new this.Helper(this.ctx); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用 this.app.ViewHelper
ok, 我改回来 |
只改了文档,我合并了 |
* master: (37 commits) docs: fix quickstart typo (#266) docs: add http client debug docs (#265) docs: modify and fix 3 points (#264) docs(intro): improve decription (#263) docs: fix docs site version (#262) docs: Fix typo. (#261) docs: review 1st version docs (#257) fix: typo conext -> context (#259) docs: contributing && readme && deps (#253) docs: fix quickstart link in index.html (#256) docs: set the default locale zh-cn (#255) refactor: ctx.realStatus delegate ctx.response.realStatus (#252) docs: Add intro/index.md (#246) feat: adjust default plugins (#251) docs: add RESTful documents (#247) feat: delegate ctx.jsonp to ctx.response.jsonp (#248) chore: remove examples (#245) docs: improve mysql doc docs: add mysql doc docs: view (#228) ...
Checklist
npm test
passesAffected core subsystem(s)
Description of change