Skip to content

Commit

Permalink
fix koa2
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Jul 31, 2019
1 parent 0cc509f commit b85a53c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 56 deletions.
21 changes: 4 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,14 @@ app.use(function(req, res){
### use for koa

```javascript
var app = require('xtpl/lib/koa')(require('koa')(),{
var app = require('xtpl/lib/koa2')(new require('koa')(),{
views:'./views'
});
app.use(function*(){
yield this.render('test',{data:1});
app.use(async function(ctx){
await ctx.render('test',{data:1});
});
```

### use for koa2

```javascript
const koa = require('koa'),
app = new koa()
const xtpl = require('xtpl/lib/koa2')
xtpl(app, {
views:'./views'
});
app.use(async (ctx, next) => {
ctx.body = await ctx.render('test',{data:1});
});
```

## Example

Expand Down Expand Up @@ -252,4 +239,4 @@ https://github.com/xtemplate/xtpl/milestones

## License

xtpl is released under the MIT license.
xtpl is released under the MIT license.
55 changes: 31 additions & 24 deletions lib/koa2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ var xtpl = require('./xtpl');
var Path = require('path');

function xtplRender(path, data, option) {
return function (done) {
xtpl.render(path, data, option, done);
};
return new Promise((resolve, reject) => {
xtpl.render(path, data, option, (e, data) => {
if (e) {
reject(e);
} else {
resolve(data);
}
});
});
}

/**
Expand All @@ -30,33 +36,34 @@ function merge(target, source) {
// option.extname
// option.
module.exports = (app, option) => {
option = option || {};
var views = option.views;
var extname = option.extname || 'xtpl';
option = option || {};
var views = option.views;
var extname = option.extname || 'xtpl';

var render = (path, data, opt) => {
async function render(path, data, opt) {

var context = {};
var context = {};

// merge koa ctx.state, notice: koa < 0.14.0 have no ctx.state
merge(context, this.state || {});
merge(context, data);
// merge koa ctx.state, notice: koa < 0.14.0 have no ctx.state
merge(context, this.state || {});
merge(context, data);

var filePath;
var filePath;

if(path.charAt(0) === '/') {
filePath = path;
} else {
filePath = Path.resolve(views, path + '.' + extname);
}
if (path.charAt(0) === '/') {
filePath = path;
} else {
filePath = Path.resolve(views, path + '.' + extname);
}

let content = '';
xtplRender(filePath, context, option)((e, data) => {
content = data;
});
return content;
const html = await xtplRender(filePath, context, option);
if (!opt || opt.write !== false) {
this.type = 'html';
this.body = html;
}
return html;
}

app.context.render = render;
return app;
app.context.render = render;
return app;
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xtpl",
"version": "3.4.1",
"version": "3.4.2",
"description": "nodejs wrapper around xtemplate engine",
"author": "yiminghe <[email protected]>",
"contributors": [
Expand All @@ -14,6 +14,7 @@
"node": ">=0.10"
},
"scripts": {
"pub":"git push && npm publish",
"test": "node --harmony ./node_modules/mocha/bin/mocha --harmony -R list tests/specs",
"cover": "node --harmony ./node_modules/istanbul/lib/cli cover ./node_modules/mocha/bin/_mocha -- -R list tests/specs/"
},
Expand All @@ -35,11 +36,11 @@
"expect.js": "^0.3.1",
"express": "~3.4.8",
"istanbul": "~0.4.3",
"koa": "^1.2.0",
"koa": "^2.7.0",
"mocha": "~2.5.3",
"mocha-istanbul": "~0.2.0",
"request": "^2.72.0",
"xtemplate": "5.x",
"supertest": "~1.2.0"
"supertest": "~1.2.0",
"xtemplate": "5.x"
}
}
23 changes: 12 additions & 11 deletions tests/specs/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var xtpl = require('../../');
var path = require('path');
var xtplKoa = require('../../lib/koa');
var xtplKoa = require('../../lib/koa2');
var expect = require('expect.js');
var request = require('supertest');
var Koa=require('koa');

function normalizeSlash(path) {
if (path.indexOf('\\') !== -1) {
Expand Down Expand Up @@ -58,16 +59,16 @@ describe('xtpl', function () {
});

it('works for koa', function (done) {
var app = xtplKoa(require('koa')(), {
var app = xtplKoa(new Koa(), {
views: path.resolve(__dirname, '../fixture/')
});
app.use(function *() {
app.use(async function (ctx) {

// test for ctx.state
this.state.name = 'foo';
this.state.age = 18;
ctx.state.name = 'foo';
ctx.state.age = 18;

var html = yield* this.render('main', {
var html = await ctx.render('main', {
y: '<',
x: '>',
age: 20
Expand All @@ -81,12 +82,12 @@ describe('xtpl', function () {
});

it('works for koa and absolute path', function (done) {
var app = xtplKoa(require('koa')());
app.use(function *() {
this.state.name = 'foo';
this.state.age = 18;
var app = xtplKoa(new Koa());
app.use(async function (ctx) {
ctx.state.name = 'foo';
ctx.state.age = 18;

var html = yield* this.render(path.resolve(__dirname, '../fixture/main.xtpl'), {
var html = await ctx.render(path.resolve(__dirname, '../fixture/main.xtpl'), {
y: '<',
x: '>',
age: 20
Expand Down

0 comments on commit b85a53c

Please sign in to comment.