Gathering detailed insights and metrics for egg-view
Gathering detailed insights and metrics for egg-view
Gathering detailed insights and metrics for egg-view
Gathering detailed insights and metrics for egg-view
npm install egg-view
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
40 Stars
29 Commits
8 Forks
10 Watching
3 Branches
25 Contributors
Updated on 16 May 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
4.3%
3,223
Compared to previous day
Last week
-5.1%
20,619
Compared to previous week
Last month
10.1%
97,420
Compared to previous month
Last year
-58.4%
1,128,108
Compared to previous year
1
Base view plugin for egg
it's a plugin that has been built-in for egg.
1$ npm i egg-view --save
1// {app_root}/config/plugin.js 2exports.view = { 3 enable: true, 4 package: 'egg-view', 5};
egg-view don't have build-in view engine, So you should choose a template engine like ejs, and install egg-view-ejs plugin.
You can choose a template engine first, link ejs, so we use egg-view-ejs plugin.
egg-view
is in eggjs, so you just need configure egg-view-ejs.
1// config/plugin.js 2exports.ejs = { 3 enable: true, 4 package: 'egg-view-ejs', 5};
Configure the mapping, the file with .ejs
extension will be rendered by ejs.
1// config/config.default.js 2exports.view = { 3 mapping: { 4 '.ejs': 'ejs', 5 }, 6};
In controller, you can call ctx.render
.
1module.exports = app => { 2 return class UserController extends app.Controller { 3 async list() { 4 const { ctx } = this; 5 await ctx.render('user.ejs'); 6 } 7 }; 8};
If you call ctx.renderString
, you should specify viewEngine in viewOptions.
1module.exports = app => { 2 return class UserController extends app.Controller { 3 async list() { 4 const { ctx } = this; 5 ctx.body = await ctx.renderString('<%= user %>', { user: 'popomore' }, { 6 viewEngine: 'ejs', 7 }); 8 } 9 }; 10};
egg-view support multiple view engine, so you can use more than one template engine in one application.
If you want add another template engine like nunjucks, then you can add egg-view-nunjucks plugin.
Configure the plugin and mapping
1// config/config.default.js 2exports.view = { 3 mapping: { 4 '.ejs': 'ejs', 5 '.nj': 'nunjucks', 6 }, 7};
You can simply render the file with .nj
extension.
1await ctx.render('user.nj');
You can use egg-view' API to register a plugin.
Create a view engine class first, and implement render
and renderString
, if the template engine don't support, just throw an error. The view engine is context level, so it receive ctx in constructor
.
1// lib/view.js 2module.exports = class MyView { 3 constructor(ctx) { 4 // do some initialize 5 // get the plugin config from `ctx.app.config` 6 } 7 8 async render(fullpath, locals) { 9 return myengine.render(fullpath, locals); 10 } 11 12 async renderString() { throw new Error('not implement'); } 13};
render
and renderString
support generator function, async function, or normal function return a promise.
If the template engine only support callback, you can wrap it by Promise.
1class MyView { 2 render(fullpath, locals) { 3 return new Promise((resolve, reject) => { 4 myengine.render(fullpath, locals, (err, result) => { 5 if (err) { 6 reject(err); 7 } else { 8 resolve(result); 9 } 10 }); 11 }); 12 } 13};
These methods receive three arguments, renderString
will pass tpl as the first argument instead of name in render
.
render(name, locals, viewOptions)
app/view
by default)config/config.default.js
. Plugin should implement it if it has config.
When you implement view engine, you will receive this options from render
, the options contain:
renderString(tpl, locals, viewOptions)
renderString
render
render
After define a view engine, you can register it.
1// app.js 2module.exports = app => { 3 app.view.use('myName', require('./lib/view')); 4};
You can define a view engine name, normally it's a template name.
Define plugin name and depend on egg-view
1{ 2 "eggPlugin": { 3 "name": "myName", 4 "dependencies": [ "view" ] 5 } 6}
Set default config in config/config.default.js
, the name is equals to plugin name.
1exports.myName = {},
See some examples
Root is ${baseDir}/app/view
by default, but you can define multiple directory, seperated by ,
. egg-view will find a file from all root directories.
1module.exports = appInfo => { 2 const baseDir = appInfo.baseDir; 3 return { 4 view: { 5 root: `${baseDir}/app/view,${baseDir}/app/view2` 6 } 7 } 8}
When render a file, you should specify a extension that let egg-view know whitch engine you want to use. However you can define defaultExtension
without write the extension.
1// config/config.default.js 2exports.view = { 3 defaultExtension: '.html', 4}; 5 6// controller 7module.exports = app => { 8 return class UserController extends app.Controller { 9 async list() { 10 const { ctx } = this; 11 // render user.html 12 await ctx.render('user'); 13 } 14 }; 15};
If you are using renderString
, you should specify viewEngine in view config, see example above.
However, you can define defaultViewEngine
without set each time.
1// config/config.default.js 2exports.view = { 3 defaultViewEngine: 'ejs', 4};
see config/config.default.js for more detail.
Please open an issue here.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 existing vulnerabilities detected
Reason
Found 14/29 approved changesets -- score normalized to 4
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More