Nixon's Pants Missing

    koa-generic-views

    0.1.1 • Public • Published

    koa-generic-views

    generic view interface for koa

    Build Status Coverage Status npm version npm downloads npm license

    Install

    npm i koa-generic-views --save

    API

    var views = require('koa-generic-views');
    views(app,options);

    Options

    • viewRoot: set the root path
    • defaultExt: set the default extension
    • cache: when final locals don't have cache field, get cache flag from opts.cache, then get cache from process.env.NODE_ENV

    Example

    var koa = require('koa');
    var app = koa();
    var jade = require('jade');
    require('koa-generic-views')(app,{
      defaultExt: 'jade'
    });
     
    // reutrn plain values
    app.engine('jade', function(view,locals){
      return jade.renderFile(view, locals) // sync render, do not use in production
    });
     
    // or Promise
    app.engine('jade',function(views,locals){
      return new Promise(function(resolve,reject){
        jade.renderFile(views,locals,function(err,res){
          if(err){
            return reject(err);
          }
          resolve(res);
        })
      })
    });
     
    // or simply use bluebird, Promise.promisify
    var Promise = require('bluebird');
    app.engine('jade',Promise.promisify(jade.renderFile,jade));
     
    app.use(ctx => {
      return this.render('template');
    });

    app.engine('ext',engine); engine should return a value or a Promise so use Promise.promisify with bluebird is also supported.

    Why

    why not koa-views?

    koa-views -> co-views -> co-render -> consolidate.js

    Any engine not supported in consolidate need to PR to consolidate.I hate centerlized.

    Changelog

    CHANGELOG.md

    License

    the MIT License http://magicdawn.mit-license.org

    Keywords

    Install

    npm i koa-generic-views

    DownloadsWeekly Downloads

    11

    Version

    0.1.1

    License

    MIT

    Last publish

    Collaborators

    • magicdawn