Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
@midwayjs/view
Advanced tools
Base view component for midway.
@midwayjs/view don't have build-in view engine, So you should choose a template engine like ejs, and install @midwayjs/view-ejs
.
View component will be auto install and enable by import view-ejs
.
$ npm i @midwayjs/view-ejs --save
First, import component in src/configuration.ts
.
import { Configuration } from '@midwayjs/core';
import * as view from '@midwayjs/view-ejs';
import { join } from 'path'
@Configuration({
imports: [
// ...
view
],
importConfigs: [
join(__dirname, 'config')
]
})
export class ContainerLifeCycle {
}
Configure the mapping, the file with .ejs
extension will be rendered by ejs.
// src/config/config.default.ts
export const view = {
defaultViewEngine: 'ejs',
mapping: {
'.ejs': 'ejs',
},
};
// ejs config
export const ejs = {};
In controller, you can call ctx.render
.
import { Inject, Provide } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
@Controller('/')
export class HomeController {
@Inject()
ctx: Context;
@Get('/')
async render(){
await this.ctx.render('hello.ejs', {
data: 'world',
});
}
}
@midwayjs/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 @midwayjs/view-nunjucks component.
Configure the plugin and mapping
// src/config/config.default.ts
export const view = {
mapping: {
'.ejs': 'ejs',
'.nj': 'nunjucks',
},
};
You can simply render the file with .nj extension.
await this.ctx.render('user.nj');
Create a view engine class first, and implement render and renderString, if the template engine don't support, just throw an error.
// lib/view.ts
import { Provide } from '@midwayjs/core';
@Provide()
export class MyView {
@Config('xxxx')
viewConfig;
async render(fullpath, locals) {
return myengine.render(fullpath, locals);
}
async renderString() { throw new Error('not implement'); }
};
These methods receive three arguments, renderString
will pass tpl as the first argument instead of name in render
.
render(name, locals, viewOptions)
/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.
// src/configuration.ts
import { Configuration, Inject, Provide } from '@midwayjs/core';
import * as koa from '@midwayjs/koa';
import * as view from '@midwayjs/view';
import { MyView } from './lib/my';
@Configuration({
imports: [koa, view],
importConfigs: [join(__dirname, 'config')]
})
export class AutoConfiguration {
@Inject()
viewManager: view.ViewManager;
async onReady(){
this.viewManager.use('ejs', MyView);
}
}
You can define a view engine name, normally it's a template name.
Root is ${appDir}/view
by default, but you can define multiple directory.
export default appInfo => {
const appDir = appInfo.appDir;
return {
view: {
rootDir: {
default: `${appDir}/view`,
anotherDir: `${appDir}/view2`
}
}
}
}
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.
// src/config/config.default.ts
export const view = {
defaultExtension: '.html',
};
// controller
import { Inject, Provide } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
@Controller('/')
export class HomeController {
@Inject()
ctx: Context;
@Get('/')
async render(){
// render user.html
await this.ctx.render('user');
}
}
If you are using renderString
, you should specify viewEngine in view config, see example above.
However, you can define defaultViewEngine
without set each time.
// config/config.default.js
export const view = {
defaultViewEngine: 'ejs',
};
FAQs
Midway Component for render view
The npm package @midwayjs/view receives a total of 294 weekly downloads. As such, @midwayjs/view popularity was classified as not popular.
We found that @midwayjs/view demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.