mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: crash when express views set to string
This commit is contained in:
@@ -102,13 +102,14 @@ var _engine = {
|
||||
}
|
||||
});
|
||||
},
|
||||
express: function(renderOption) {
|
||||
renderOption = renderOption || {};
|
||||
express: function(opts) {
|
||||
opts = opts || {};
|
||||
var self = this;
|
||||
return function(filePath, options, callback) {
|
||||
assert(_.isArray(this.root), 'illegal views root, are you using express.js?');
|
||||
renderOption.root = this.root;
|
||||
self.renderFile(filePath, options, renderOption)
|
||||
return function(filePath, ctx, callback) {
|
||||
assert(_.isArray(this.root) || _.isString(this.root),
|
||||
'illegal views root, are you using express.js?');
|
||||
opts.root = this.root;
|
||||
self.renderFile(filePath, ctx, opts)
|
||||
.then(html => callback(null, html))
|
||||
.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() {
|
||||
var app, engine;
|
||||
|
||||
before(function() {
|
||||
mock({
|
||||
'/root/foo.html': 'foo',
|
||||
'/views/name.html': 'My name is {{name}}.',
|
||||
'/views/include.html': '{% include file %}',
|
||||
'/partials/bar.html': 'bar'
|
||||
});
|
||||
beforeEach(function() {
|
||||
app = express();
|
||||
engine = Liquid({
|
||||
root: '/root',
|
||||
extname: '.html'
|
||||
});
|
||||
|
||||
app.set('views', ['/views', '/partials']);
|
||||
app.set('view engine', 'html');
|
||||
app.engine('html', engine.express());
|
||||
|
||||
app.get('/name', function(req, res) {
|
||||
res.render('name', {
|
||||
name: 'harttle'
|
||||
});
|
||||
});
|
||||
app.get('/include/:file', function(req, res) {
|
||||
res.render('include', {
|
||||
file: req.params.file
|
||||
});
|
||||
});
|
||||
app.get('/name', (req, res) => res.render('name', {
|
||||
name: 'harttle'
|
||||
}));
|
||||
app.get('/include/:file', (req, res) => res.render('include', {
|
||||
file: req.params.file
|
||||
}));
|
||||
});
|
||||
after(function(){
|
||||
after(function() {
|
||||
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')
|
||||
.expect('My name is harttle.')
|
||||
.expect(200, done);
|
||||
@@ -51,22 +42,51 @@ describe('engine#express()', function() {
|
||||
var file = '/not-exist.html';
|
||||
var ctx = {};
|
||||
engine.express().call(view, file, ctx, function(err) {
|
||||
try{
|
||||
try {
|
||||
expect(err.code).to.equal('ENOENT');
|
||||
expect(err.message).to.match(/Failed to lookup/);
|
||||
done();
|
||||
}
|
||||
catch(e){
|
||||
} catch (e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
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')
|
||||
.expect('foo')
|
||||
.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')
|
||||
.expect('bar')
|
||||
.expect(200, done);
|
||||
|
||||
+14
-17
@@ -139,10 +139,13 @@ describe('liquid', function() {
|
||||
});
|
||||
describe('cache', function() {
|
||||
it('should be disabled by default', function() {
|
||||
mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
});
|
||||
return engine.renderFile('files/foo', ctx).should.eventually.equal('bar');
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(x => mock({
|
||||
'/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() {
|
||||
engine = Liquid({
|
||||
@@ -150,19 +153,13 @@ describe('liquid', function() {
|
||||
extname: '.html',
|
||||
cache: true
|
||||
});
|
||||
return engine.renderFile('files/foo', ctx)
|
||||
.then((result) => {
|
||||
return expect(result).to.equal('foo');
|
||||
})
|
||||
.then((result) => {
|
||||
mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
});
|
||||
return engine.renderFile('files/foo', ctx);
|
||||
})
|
||||
.then((result) => {
|
||||
return expect(result).to.equal('foo');
|
||||
});
|
||||
return engine.renderFile('files/foo')
|
||||
.then(x => expect(x).to.equal('foo'))
|
||||
.then(x => mock({
|
||||
'/root/files/foo.html': 'bar'
|
||||
}))
|
||||
.then(x => engine.renderFile('files/foo'))
|
||||
.then(x => expect(x).to.equal('foo'));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,7 +54,7 @@ describe('util/underscore', function() {
|
||||
});
|
||||
});
|
||||
describe('.echo()', function(){
|
||||
it('should be transparent when called', function() {
|
||||
it('should be transparent', function() {
|
||||
expect(_.echo('foo')('bar')).to.equal('bar');
|
||||
});
|
||||
it('should log the arguments', function() {
|
||||
|
||||
Reference in New Issue
Block a user