diff --git a/index.js b/index.js index 6b4370912..0f46765fe 100644 --- a/index.js +++ b/index.js @@ -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)); }; diff --git a/src/express.js b/src/express.js deleted file mode 100644 index 208fb5bdc..000000000 --- a/src/express.js +++ /dev/null @@ -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#', '' + options.title + '') - .replace('#message#', '

' + options.message + '

'); - return callback(null, rendered); - }); - } - return express; -}; diff --git a/test/express.js b/test/express.js index 19b5cd2f3..aadb14adf 100644 --- a/test/express.js +++ b/test/express.js @@ -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); diff --git a/test/liquid.js b/test/liquid.js index e478df69a..92662ddb9 100644 --- a/test/liquid.js +++ b/test/liquid.js @@ -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')); }); }); }); diff --git a/test/util/underscore.js b/test/util/underscore.js index 91e0a112f..0cb39ee5f 100644 --- a/test/util/underscore.js +++ b/test/util/underscore.js @@ -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() {