UNPKG

@eggjs/view

Version:

Base view plugin for egg

45 lines (44 loc) 2.14 kB
import { Context } from '@eggjs/core'; import { ContextView } from '../../lib/context_view.js'; import { RenderOptions } from '../../lib/view_manager.js'; declare const VIEW: unique symbol; export default class ViewContext extends Context { [VIEW]: ContextView; /** * Render a file by view engine, then set to body * @param {String} name - the file path based on root * @param {Object} [locals] - data used by template * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine */ render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>; /** * Render a file by view engine and return it * @param {String} name - the file path based on root * @param {Object} [locals] - data used by template * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine * @return {Promise<String>} result - return a promise with a render result */ renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>; /** * Render template string by view engine and return it * @param {String} tpl - template string * @param {Object} [locals] - data used by template * @param {Object} [options] - view options, you can use `options.viewEngine` to specify view engine * @return {Promise<String>} result - return a promise with a render result */ renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>; /** * View instance that is created every request * @member {ContextView} Context#view */ get view(): ContextView; } declare module '@eggjs/core' { interface Context { view: ContextView; render(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<void>; renderView(name: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>; renderString(tpl: string, locals?: Record<string, any>, options?: RenderOptions): Promise<string>; } } export {};