3.16.1 • Published 8 days ago

@midwayjs/view v3.16.1

Weekly downloads
-
License
MIT
Repository
-
Last release
8 days ago

midway-view

Base view component for midway.

Install

@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

Usage

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',
    });
  }
}

Use multiple view engine

@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');

Write a view engine

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)

  • name: the file path that can resolve from root (/view by default)
  • locals: data used by template
  • 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. When you implement view engine, you will receive this options from render, the options contain:
    • root: it will resolve the name to full path, but seperating root and name in viewOptions.
    • name: the original name when call render
    • locals: the original locals when call render

renderString(tpl, locals, viewOptions)

  • tpl: the template string instead of the file, using in renderString
  • locals: same as render
  • viewOptions: same as render

Register

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.

Configuration

Root

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`
      }
    }
  }
}

defaultExtension

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');
  }
}

viewEngine and defaultViewEngine

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',
};

License

MIT

3.16.1

8 days ago

3.16.0

10 days ago

3.15.11

22 days ago

3.15.10

27 days ago

3.15.8

1 month ago

3.15.6

2 months ago

3.15.2

2 months ago

3.15.1

3 months ago

3.15.0

3 months ago

3.14.12

3 months ago

3.14.11

3 months ago

3.14.7

4 months ago

3.14.4

4 months ago

3.14.3

4 months ago

3.14.0

4 months ago

3.13.9

5 months ago

3.13.8

5 months ago

3.13.7

5 months ago

3.13.6

5 months ago

3.13.5

5 months ago

3.13.4

6 months ago

3.13.3

6 months ago

3.12.1

9 months ago

3.12.0

9 months ago

3.12.10

6 months ago

3.13.0

6 months ago

3.12.3

9 months ago

3.12.2

9 months ago

3.12.8

7 months ago

3.11.12

10 months ago

3.11.15

10 months ago

3.11.9

12 months ago

3.11.11

11 months ago

3.11.10

12 months ago

3.10.15

1 year ago

3.11.4

1 year ago

3.11.3

1 year ago

3.11.6

1 year ago

3.11.5

1 year ago

3.11.0

1 year ago

3.11.1

1 year ago

3.10.10

1 year ago

3.10.13

1 year ago

3.10.11

1 year ago

3.10.5

1 year ago

3.10.4

1 year ago

3.10.7

1 year ago

3.10.6

1 year ago

3.10.9

1 year ago

3.10.1

1 year ago

3.10.0

1 year ago

3.10.3

1 year ago

3.9.9

1 year ago

3.9.0

1 year ago

3.8.0

1 year ago

3.7.3

2 years ago

3.6.0

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

3.4.13

2 years ago

3.5.3

2 years ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.0-beta.7

2 years ago

3.4.0-beta.6

2 years ago

3.4.0-beta.11

2 years ago

3.4.0-beta.5

2 years ago

3.4.0-beta.12

2 years ago

3.4.0-beta.4

2 years ago

3.4.0-beta.10

2 years ago

3.4.0-beta.9

2 years ago

3.4.0-beta.8

2 years ago

3.4.0-beta.3

2 years ago

3.4.0-beta.2

2 years ago

3.4.0-beta.1

2 years ago

3.4.0

2 years ago

3.4.4

2 years ago

3.4.3

2 years ago

3.4.1

2 years ago

3.4.10

2 years ago

3.4.11

2 years ago

3.4.12

2 years ago

3.4.7

2 years ago

3.4.6

2 years ago

3.4.9

2 years ago

3.3.6

2 years ago

3.1.7-alpha.0

2 years ago

3.2.2

2 years ago

3.2.1

2 years ago

3.2.0

2 years ago

3.3.1

2 years ago

3.3.0

2 years ago

3.3.5

2 years ago

3.3.4

2 years ago

3.1.6

2 years ago

3.3.2

2 years ago

3.1.2

2 years ago

3.0.13

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.1.5

2 years ago

3.0.9

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.10

2 years ago

3.0.2

2 years ago

3.0.11

2 years ago

3.0.1

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.6

2 years ago

3.0.5

2 years ago

2.14.6

2 years ago

3.0.0

2 years ago

2.14.2

2 years ago

3.0.4-beta.1

2 years ago

3.0.0-beta.12

2 years ago

3.0.0-beta.13

2 years ago

3.0.0-beta.14

2 years ago

3.0.0-beta.15

2 years ago

3.0.0-beta.16

2 years ago

3.0.0-beta.17

2 years ago

3.0.0-beta.1

3 years ago

3.0.0-beta.3

2 years ago

3.0.0-beta.2

3 years ago

3.0.0-beta.5

2 years ago

3.0.0-beta.4

2 years ago

3.0.0-beta.7

2 years ago

2.14.0

2 years ago

3.0.0-beta.6

2 years ago

3.0.0-beta.9

2 years ago

3.0.0-beta.8

2 years ago

3.0.0-beta.10

2 years ago

3.0.0-beta.11

2 years ago

2.13.4

3 years ago

2.13.3

3 years ago

2.13.1

3 years ago

2.13.0

3 years ago