Skip to content

Commit

Permalink
support absolute path
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Sep 28, 2016
1 parent bd3a019 commit 06bd0e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function merge(target, source) {
// option.extname
// option.
module.exports = function (app, option) {
option = option || {};
var views = option.views;
var extname = option.extname || 'xtpl';

Expand All @@ -41,7 +42,15 @@ module.exports = function (app, option) {
merge(context, this.state || {});
merge(context, data);

var html = yield xtplRender(Path.resolve(views, path + '.' + extname), context, option);
var filePath;

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

var html = yield xtplRender(filePath, context, option);
if (!opt || opt.write !== false) {
this.type = 'html';
this.body = html;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xtpl",
"version": "3.2.0",
"version": "3.3.0",
"description": "nodejs wrapper around xtemplate engine",
"author": "yiminghe <yiminghe@gmail.com>",
"contributors": [
Expand Down
19 changes: 19 additions & 0 deletions tests/specs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,23 @@ describe('xtpl', function () {
.expect(200)
.expect('<&gt;foo20', done);
});

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 html = yield* this.render(path.resolve(__dirname, '../fixture/main.xtpl'), {
y: '<',
x: '>',
age: 20
});
expect(html).to.be('<&gt;foo20');
});
request(app.listen())
.get('/')
.expect(200)
.expect('<&gt;foo20', done);
});
});

0 comments on commit 06bd0e9

Please sign in to comment.