Skip to content

opentpl/otpl-node

Repository files navigation

otpl-node NPM version License Build Status

OTPL for Nodejs (Typescript).

Documentation

Installation

$ npm install opentpl --save

Usage

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');
});

Changelog

Details changes for each release are documented in the release notes.

Contribution

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.

License

Apache-2.0

Releases

No releases published

Packages

No packages published