Skip to content

Commit

Permalink
Merge pull request hapijs#1856 from jas/handler-view-runtime-options
Browse files Browse the repository at this point in the history
Allow view options override on handler object
  • Loading branch information
Eran Hammer committed Aug 13, 2014
2 parents 3d33b46 + c7c416e commit 355d45f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 3 deletions.
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

0 comments on commit 355d45f

Please sign in to comment.