OTPL for Nodejs (Typescript).
$ npm install opentpl --save
const otpl = require('opentpl')
otpl.config(__dirname,{
debug:true,
...
})
//使用 koa 框架为例
const app = require('koa')();
//注册中间件
app.use(function *(next){
this.otpl = function* (view, data) {
let ctx = this
yield new Promise((resolve, reject) => {
let callback = function (err, rendered) {
if (err) {
rendered = err.message;
console.log('render error:', err)
}
this.type = 'text/html;charset=UTF-8'
ctx.body = rendered
resolve(rendered)
}
otpl.render(view, data, callback)
})
}
yield next;
});
app.use(function *(next){
this.otpl('index',{foo:'bar'}) //template file: /views/index.otpl.html
})
app.listen(3000,function(err){
console.log('serve on port: 3000');
});
Details changes for each release are documented in the release notes.
If you find a bug or want to contribute to the code or documentation, you can help by submitting an issue or a pull request.