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

Allow view options override on handler object #1856

Merged
merged 1 commit into from
Aug 13, 2014
Merged
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
1 change: 1 addition & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ The following options are available when adding a route:
- `params` - maps to `request.params`.
- `query` - maps to `request.query`.
- `pre` - maps to `request.pre`.
- `options` - optional object used to override the server's [`views`](#server.config.views) configuration.

- `config` - additional route configuration (the `config` options allows splitting the route information from its implementation):
- `handler` - an alternative location for the route handler function. Same as the `handler` option in the parent level. Can only
Expand Down
3 changes: 2 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ internals['view handler'] = Joi.alternatives([
Joi.string(),
Joi.object({
template: Joi.string(),
context: Joi.object()
context: Joi.object(),
options: Joi.object()
})
]);

Expand Down
5 changes: 3 additions & 2 deletions lib/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ exports.handler = function (route, options) {

var settings = { // Shallow copy to allow making dynamic changes to context
template: options.template,
context: options.context
context: options.context,
options: options.options
};

return function (request, reply) {
Expand All @@ -379,7 +380,7 @@ exports.handler = function (route, options) {
};
}

reply.view(settings.template, context);
reply.view(settings.template, context, settings.options);
};
};

Expand Down
1 change: 1 addition & 0 deletions test/templates/valid/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello
20 changes: 20 additions & 0 deletions test/views.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,26 @@ describe('Views', function () {
});
});

it('handles custom options', function (done) {

var options = {
views: {
engines: { html: require('handlebars') },
path: __dirname + '/templates',
layoutPath: __dirname + '/templates/layout'
}
};

var server = new Hapi.Server(options);

server.route({ method: 'GET', path: '/', handler: { view: { template: 'valid/options', options: { layout: 'elsewhere' } } } });
server.inject('/', function (res) {

expect(res.result).to.contain('+hello');
done();
});
});

it('includes prerequisites in the default view context', function (done) {

var pre = function (request, reply) {
Expand Down