Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options for Engines #3114

Open
wants to merge 2 commits into
base: 5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/view-constructor/github-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = GithubView;
function GithubView(name, options){
this.name = name;
options = options || {};
this.engine = options.engines[extname(name)];
this.engine = options.engines[extname(name)].callback;
// "root" is the app.set('views') setting, however
// in your own implementation you could ignore this
this.path = '/' + options.root + '/master/' + name;
Expand Down
8 changes: 6 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ app.route = function route(path) {
* @public
*/

app.engine = function engine(ext, fn) {
app.engine = function engine(ext, fn, options) {
if (typeof fn !== 'function') {
throw new Error('callback function required');
}
Expand All @@ -296,7 +296,10 @@ app.engine = function engine(ext, fn) {
: ext;

// store engine
this.engines[extension] = fn;
this.engines[extension] = {
callback: fn,
options: options || this.get('view engine options') || {}
};

return this;
};
Expand Down Expand Up @@ -554,6 +557,7 @@ app.render = function render(name, options, callback) {

view = new View(name, {
defaultEngine: this.get('view engine'),
defaultEngineOptions: this.get('view engine options') || {},
root: this.get('views'),
engines: engines
});
Expand Down
11 changes: 8 additions & 3 deletions lib/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function View(name, options) {
var opts = options || {};

this.defaultEngine = opts.defaultEngine;
this.defaultEngineOptions = opts.defaultEngineOptions;
this.ext = extname(name);
this.name = name;
this.root = opts.root;
Expand All @@ -75,14 +76,18 @@ function View(name, options) {

if (!opts.engines[this.ext]) {
// load engine
opts.engines[this.ext] = require(this.ext.substr(1)).__express;
opts.engines[this.ext] = {
callback: require(this.ext.substr(1)).__express,
options: this.defaultEngineOptions
};
}

// store loaded engine
this.engine = opts.engines[this.ext];
var engine = opts.engines[this.ext];
this.engine = engine.callback;

// lookup path
this.path = this.lookup(fileName);
this.path = engine.options.noFileSystem ? name : this.lookup(fileName);
}

/**
Expand Down
45 changes: 44 additions & 1 deletion test/app.engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function render(path, options, fn) {
}

describe('app', function(){
describe('.engine(ext, fn)', function(){
describe('.engine(ext, fn, options)', function(){
it('should map a template engine', function(done){
var app = express();

Expand Down Expand Up @@ -76,5 +76,48 @@ describe('app', function(){
done();
})
})

it('should throw when no options are specified and the file does not exist', function(done){
var app = express();

app.set('views', __dirname + '/fixtures');
app.engine('.html', render);
app.set('view engine', '.html');

app.render('nonexistent', function(err, str){
if (err) return done();
done(new Error('No Error was thrown despite missing file'));
})
})

it('should throw when noFileSystem is explicitly set to false and the file does not exist', function(done){
var app = express();

app.set('views', __dirname + '/fixtures');
app.engine('.html', render, { noFileSystem: false });
app.set('view engine', '.html');

app.render('nonexistent', function(err, str){
if (err) return done();
done(new Error('No Error was thrown despite missing file'));
})
})

it('should work when noFileSystem is set to true and the file does not exist', function(done){
var app = express();
var fileName = 'nonexistent';

app.set('views', __dirname + '/fixtures');
app.engine('.html', function(name, options, fn) {
name.should.equal(fileName);
done();
}, { noFileSystem: true });
app.set('view engine', '.html');

app.render(fileName, function(err, str){
if (err) return done(err);
done();
})
})
})
})