easytemplatejs-koa
Version:
EasyTemplateJS Template Engine middleware for koa!
16 lines (15 loc) • 934 B
JavaScript
/**
* EasyTemplateJS-koa
*
* Version 3.0.1
*
* http://easyproject.cn
* https://github.com/ushelp
*
* Copyright 2012 Ray [ inthinkcolor@gmail.com ]
* Released under the MIT license
*
* [Support AMD, CMD, CommonJS, Node.js, koa, koajs, EasyTemplateJS]
*
*/
const path=require("path"),fs=require("fs");var Et=require("easytemplatejs"),defaultSettings={cache:!0,enableScript:!0,enableStyle:!0},cacheTpl={};exports=module.exports=function(a,b){a.context.render||(b=Object.assign({},defaultSettings,b),Et.enableScript=b.enableScript,Et.enableStyle=b.enableScript,a.context.render=function(a,c){const d=this,e=Object.assign({},d.state,c);var f=path.join(b.views,a+"."+b.ext);return new Promise(function(a,c){fs.readFile(f,"utf-8",function(g,h){if(g)c(g);else{var i;b.cache?(cacheTpl[f]||(cacheTpl[f]=Et.template(h)),i=cacheTpl[f]):i=Et.template(h);var j=i(e);d.type="html",d.body=j,a(j)}})})})},exports.templateEngine=Et;