@eggjs/view
Version:
Base view plugin for egg
58 lines (57 loc) • 2.01 kB
TypeScript
import type { Context, EggCore } from '@eggjs/core';
import type { ViewConfig } from '../config/config.default.js';
export interface ViewManagerConfig extends Omit<ViewConfig, 'root'> {
root: string[];
}
export type PlainObject<T = any> = {
[key: string]: T;
};
export interface RenderOptions extends PlainObject {
name?: string;
root?: string;
locals?: PlainObject;
viewEngine?: string;
}
export interface ViewEngine {
render: (name: string, locals?: Record<string, any>, options?: RenderOptions) => Promise<string>;
renderString: (tpl: string, locals?: Record<string, any>, options?: RenderOptions) => Promise<string>;
}
export type ViewEngineClass = new (app: Context) => ViewEngine;
/**
* ViewManager will manage all view engine that is registered.
*
* It can find the real file, then retrieve the view engine based on extension.
* The plugin just register view engine using {@link ViewManager#use}
*/
export declare class ViewManager extends Map<string, ViewEngineClass> {
config: ViewManagerConfig;
extMap: Map<string, string>;
fileMap: Map<string, string>;
/**
* @param {Application} app - application instance
*/
constructor(app: EggCore);
/**
* This method can register view engine.
*
* You can define a view engine class contains two method, `render` and `renderString`
*
* ```js
* class View {
* render() {}
* renderString() {}
* }
* ```
* @param {String} name - the name of view engine
* @param {Object} viewEngine - the class of view engine
*/
use(name: string, viewEngine: ViewEngineClass): void;
/**
* Resolve the path based on the given name,
* if the name is `user.html` and root is `app/view` (by default),
* it will return `app/view/user.html`
* @param {String} name - the given path name, it's relative to config.root
* @return {String} filename - the full path
*/
resolve(name: string): Promise<string>;
}