mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 06:50:47 -07:00
fix: crash when express views set to string
This commit is contained in:
@@ -102,13 +102,14 @@ var _engine = {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
express: function(renderOption) {
|
express: function(opts) {
|
||||||
renderOption = renderOption || {};
|
opts = opts || {};
|
||||||
var self = this;
|
var self = this;
|
||||||
return function(filePath, options, callback) {
|
return function(filePath, ctx, callback) {
|
||||||
assert(_.isArray(this.root), 'illegal views root, are you using express.js?');
|
assert(_.isArray(this.root) || _.isString(this.root),
|
||||||
renderOption.root = this.root;
|
'illegal views root, are you using express.js?');
|
||||||
self.renderFile(filePath, options, renderOption)
|
opts.root = this.root;
|
||||||
|
self.renderFile(filePath, ctx, opts)
|
||||||
.then(html => callback(null, html))
|
.then(html => callback(null, html))
|
||||||
.catch(e => callback(e));
|
.catch(e => callback(e));
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +0,0 @@
|
|||||||
const Liquid = require('..');
|
|
||||||
|
|
||||||
module.exports = function() {
|
|
||||||
var engine = Liquid({
|
|
||||||
root: '/root/',
|
|
||||||
extname: '.html'
|
|
||||||
});
|
|
||||||
|
|
||||||
function express(filePath, options, callback) {
|
|
||||||
fs.readFile(filePath, function(err, content) {
|
|
||||||
if (err) return callback(new Error(err));
|
|
||||||
// this is an extremely simple template engine
|
|
||||||
var rendered = content.toString().replace('#title#', '<title>' + options.title + '</title>')
|
|
||||||
.replace('#message#', '<h1>' + options.message + '</h1>');
|
|
||||||
return callback(null, rendered);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return express;
|
|
||||||
};
|
|
||||||
+44
-24
@@ -8,38 +8,29 @@ const Liquid = require('..');
|
|||||||
describe('engine#express()', function() {
|
describe('engine#express()', function() {
|
||||||
var app, engine;
|
var app, engine;
|
||||||
|
|
||||||
before(function() {
|
beforeEach(function() {
|
||||||
mock({
|
|
||||||
'/root/foo.html': 'foo',
|
|
||||||
'/views/name.html': 'My name is {{name}}.',
|
|
||||||
'/views/include.html': '{% include file %}',
|
|
||||||
'/partials/bar.html': 'bar'
|
|
||||||
});
|
|
||||||
app = express();
|
app = express();
|
||||||
engine = Liquid({
|
engine = Liquid({
|
||||||
root: '/root',
|
root: '/root',
|
||||||
extname: '.html'
|
extname: '.html'
|
||||||
});
|
});
|
||||||
|
|
||||||
app.set('views', ['/views', '/partials']);
|
|
||||||
app.set('view engine', 'html');
|
app.set('view engine', 'html');
|
||||||
app.engine('html', engine.express());
|
app.engine('html', engine.express());
|
||||||
|
|
||||||
app.get('/name', function(req, res) {
|
app.get('/name', (req, res) => res.render('name', {
|
||||||
res.render('name', {
|
name: 'harttle'
|
||||||
name: 'harttle'
|
}));
|
||||||
});
|
app.get('/include/:file', (req, res) => res.render('include', {
|
||||||
});
|
file: req.params.file
|
||||||
app.get('/include/:file', function(req, res) {
|
}));
|
||||||
res.render('include', {
|
|
||||||
file: req.params.file
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
after(function(){
|
after(function() {
|
||||||
mock.restore();
|
mock.restore();
|
||||||
});
|
});
|
||||||
it('should render templates', function(done) {
|
it('should render express views', function(done) {
|
||||||
|
mock({ '/views/name.html': 'My name is {{name}}.' });
|
||||||
|
app.set('views', ['/views']);
|
||||||
request(app).get('/name')
|
request(app).get('/name')
|
||||||
.expect('My name is harttle.')
|
.expect('My name is harttle.')
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
@@ -51,22 +42,51 @@ describe('engine#express()', function() {
|
|||||||
var file = '/not-exist.html';
|
var file = '/not-exist.html';
|
||||||
var ctx = {};
|
var ctx = {};
|
||||||
engine.express().call(view, file, ctx, function(err) {
|
engine.express().call(view, file, ctx, function(err) {
|
||||||
try{
|
try {
|
||||||
expect(err.code).to.equal('ENOENT');
|
expect(err.code).to.equal('ENOENT');
|
||||||
expect(err.message).to.match(/Failed to lookup/);
|
expect(err.message).to.match(/Failed to lookup/);
|
||||||
done();
|
done();
|
||||||
}
|
} catch (e) {
|
||||||
catch(e){
|
|
||||||
done(e);
|
done(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it('should respect root option when lookup', function(done) {
|
it('should respect root option when lookup', function(done) {
|
||||||
|
mock({
|
||||||
|
'/root/foo.html': 'foo',
|
||||||
|
'/views/include.html': '{% include file %}'
|
||||||
|
});
|
||||||
|
app.set('views', ['/views']);
|
||||||
request(app).get('/include/foo')
|
request(app).get('/include/foo')
|
||||||
.expect('foo')
|
.expect('foo')
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
});
|
});
|
||||||
it('should respect express settings.views when lookup', function(done) {
|
it('should respect express views (Array) when lookup', function(done) {
|
||||||
|
mock({
|
||||||
|
'/views/include.html': '{% include file %}',
|
||||||
|
'/partials/bar.html': 'bar'
|
||||||
|
});
|
||||||
|
app.set('views', ['/views', '/partials']);
|
||||||
|
request(app).get('/include/bar')
|
||||||
|
.expect('bar')
|
||||||
|
.expect(200, done);
|
||||||
|
});
|
||||||
|
it('should respect express views (String) when lookup', function(done) {
|
||||||
|
mock({
|
||||||
|
'/views/include.html': '{% include file %}',
|
||||||
|
'/views/bar.html': 'bar'
|
||||||
|
});
|
||||||
|
app.set('views', '/views');
|
||||||
|
request(app).get('/include/bar')
|
||||||
|
.expect('bar')
|
||||||
|
.expect(200, done);
|
||||||
|
});
|
||||||
|
it('should respect express views (Undefined) when lookup', function(done) {
|
||||||
|
var files = {};
|
||||||
|
files[process.cwd() + '/views/include.html'] = '{% include file %}';
|
||||||
|
files[process.cwd() + '/views/bar.html'] = 'bar';
|
||||||
|
mock(files);
|
||||||
|
|
||||||
request(app).get('/include/bar')
|
request(app).get('/include/bar')
|
||||||
.expect('bar')
|
.expect('bar')
|
||||||
.expect(200, done);
|
.expect(200, done);
|
||||||
|
|||||||
+14
-17
@@ -139,10 +139,13 @@ describe('liquid', function() {
|
|||||||
});
|
});
|
||||||
describe('cache', function() {
|
describe('cache', function() {
|
||||||
it('should be disabled by default', function() {
|
it('should be disabled by default', function() {
|
||||||
mock({
|
return engine.renderFile('files/foo')
|
||||||
'/root/files/foo.html': 'bar'
|
.then(x => expect(x).to.equal('foo'))
|
||||||
});
|
.then(x => mock({
|
||||||
return engine.renderFile('files/foo', ctx).should.eventually.equal('bar');
|
'/root/files/foo.html': 'bar'
|
||||||
|
}))
|
||||||
|
.then(x => engine.renderFile('files/foo'))
|
||||||
|
.then(x => expect(x).to.equal('bar'));
|
||||||
});
|
});
|
||||||
it('should respect cache=true option', function() {
|
it('should respect cache=true option', function() {
|
||||||
engine = Liquid({
|
engine = Liquid({
|
||||||
@@ -150,19 +153,13 @@ describe('liquid', function() {
|
|||||||
extname: '.html',
|
extname: '.html',
|
||||||
cache: true
|
cache: true
|
||||||
});
|
});
|
||||||
return engine.renderFile('files/foo', ctx)
|
return engine.renderFile('files/foo')
|
||||||
.then((result) => {
|
.then(x => expect(x).to.equal('foo'))
|
||||||
return expect(result).to.equal('foo');
|
.then(x => mock({
|
||||||
})
|
'/root/files/foo.html': 'bar'
|
||||||
.then((result) => {
|
}))
|
||||||
mock({
|
.then(x => engine.renderFile('files/foo'))
|
||||||
'/root/files/foo.html': 'bar'
|
.then(x => expect(x).to.equal('foo'));
|
||||||
});
|
|
||||||
return engine.renderFile('files/foo', ctx);
|
|
||||||
})
|
|
||||||
.then((result) => {
|
|
||||||
return expect(result).to.equal('foo');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ describe('util/underscore', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe('.echo()', function(){
|
describe('.echo()', function(){
|
||||||
it('should be transparent when called', function() {
|
it('should be transparent', function() {
|
||||||
expect(_.echo('foo')('bar')).to.equal('bar');
|
expect(_.echo('foo')('bar')).to.equal('bar');
|
||||||
});
|
});
|
||||||
it('should log the arguments', function() {
|
it('should log the arguments', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user