@chuchur/koa-views

Template rendering middleware for koa

Usage no npm install needed!

<script type="module">
  import chuchurKoaViews from 'https://cdn.skypack.dev/@chuchur/koa-views';
</script>

README

Installation

npm install @chuchur/koa-views --save

Useage

app.use(views(path,options))

options

{
  extension:'' //defalut 'html',supported more extension like 'ng','tmp'
  engine:'nunjucks'  //default empty , List of supported engines => https://github.com/tj/onsolidate.js#supported-template-engines
  pretty: true, //default false  ,use the `pretty` package in the meanwhile
  minify: {...}  // defalut { minifyCSS: true, minifyJS: true, minifyURLs: true,collapseBooleanAttributes: true, collapseWhitespace:true}  //more options https://github.com/kangax/html-minifier
}

pretty and minify only one can be uesd,if pretty is true, minify will be invalid

Example

var views = require('koa-views');

// Must be used before any router is used
app.use(views(__dirname + '/views', {
  extension:'ng',
  engine:'nunjucks',
  pretty:true, 
}));

app.use(async function (ctx) {
  ctx.state = {
    session: this.session,
    title: 'app'
  };

  await ctx.render('user', {
    user: 'John'
  });
});

只是在 koa-views 的基础上做了些许改动, 支持更多扩展名,如.ng, .tmp,把pretty 移到了外面,加入了 html-minifier,更好的支持html 压缩