Skip to content
This repository has been archived by the owner on Apr 9, 2019. It is now read-only.

just a simple plugin for egg view, to render static file from public directory

License

Notifications You must be signed in to change notification settings

eggjs-community/egg-view-static

Repository files navigation

egg-view-static

NPM version build status Test coverage David deps Known Vulnerabilities npm download

A simple egg view plugin, allow you to render static html from app/public.

Install

$ npm i egg-view-static --save

Usage

// {app_root}/config/plugin.js
exports.viewStatic = {
  enable: true,
  package: 'egg-view-static',
};

Set mapping in config

// {app_root}/config/config.default.js
exports.view = {
  defaultViewEngine: 'static',
  mapping: {
    '.html': 'static',
  },
};

Render in controller

// {app_root}/app/controller/home.js
class HomeController extends Controller {
  async index() {
    // app/static/index.html
    await this.ctx.render('index.html');
  }
}

Use replaceFn to custom render with locals, see test/fixtures/apps/locals/config/config.default.js for more detail.

// {app_root}/config/config.default.js
config.viewStatic = {
  replaceFn(tpl, locals) {
    return tpl.toString().replace(/(\\)?{{ *(\w+) *}}/g, (block, skip, key) => {
      if (skip) {
        return block.substring(skip.length);
      }
      return locals.hasOwnProperty(key) ? locals[key] : block;
    });
  },
};

// {app_root}/app/controller/home.js
class HomeController extends Controller {
  async index() {
    // app/static/index.html
    await this.ctx.render('index.html', { name: 'egg' });
  }
}

Configuration

// {app_root}/config/config.default.js
exports.viewStatic = {
  // cache: true,
  // replaceFn: (tpl, locals, options) => tpl,
};
  • {Boolean} cache - whether cache file, default to true except local mode. It will share cache with egg-static LRU cache.
  • {Function} replaceFn - custom render replacement, args = (tpl, locals, options)

see config/config.default.js for more detail.

Example

Questions & Suggestions

Please open an issue here.

License

MIT

About

just a simple plugin for egg view, to render static file from public directory

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published