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

Add support for render-level partials #107

Merged
merged 1 commit into from
Feb 17, 2015
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
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,13 @@ resulting string.
pipe through the template, all helpers, and all partials. This is a side
data channel.

* `[helpers]`: Render-level helpers should be merged with (and will override)
instance and global helper functions.
* `[helpers]`: Render-level helpers that will be used instead of any
instance-level helpers; these will be merged with (and will override) any
global Handlebars helper functions.

* `[partials]`: Render-level partials that override _all_ this instance's
partials. This is used internally as an optimization to avoid re-loading all
the partials.
* `[partials]`: Render-level partials that will be used instead of any
instance-level partials. This is used internally as an optimization to avoid
re-loading all the partials.

#### `renderView(viewPath, options|callback, [callback])`

Expand Down Expand Up @@ -596,8 +597,12 @@ are rendered, and to signal this view engine on how it should behave, e.g.,
pipe through the template, all helpers, and all partials. This is a side
data channel.

* `[helpers]`: Render-level helpers should be merged with (and will override)
instance and global helper functions.
* `[helpers]`: Render-level helpers that will be merged with (and will
override) instance and global helper functions.

* `[partials]`: Render-level partials will be merged with (and will override)
instance and global partials. This should be a `{partialName: fn}` hash or
a Promise of an object with this shape.

* `[layout]`: Optional string path to the Handlebars template file to be used
as the "layout". This overrides any `defaultLayout` value. Passing a falsy
Expand Down
8 changes: 7 additions & 1 deletion examples/advanced/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var Promise = global.Promise || require('promise');

var express = require('express'),
exphbs = require('../../'), // "express-handlebars"
helpers = require('./lib/helpers');
Expand Down Expand Up @@ -89,7 +91,11 @@ app.get('/echo/:message?', exposeTemplates, function (req, res) {
message: req.params.message,

// Overrides which layout to use, instead of the defaul "main" layout.
layout: 'shared-templates'
layout: 'shared-templates',

partials: Promise.resolve({
echo: hbs.handlebars.compile('<p>ECHO: {{message}}</p>')
})
});
});

Expand Down
22 changes: 14 additions & 8 deletions lib/express-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,9 @@ ExpressHandlebars.prototype.render = function (filePath, context, options) {
]).then(function (templates) {
var template = templates[0],
partials = templates[1],
helpers = options.helpers || this.helpers,
data = options.data;

var helpers = options.helpers ||
utils.extend({}, this.handlebars.helpers, this.helpers);

return this._renderTemplate(template, context, {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the merge of this.handlebars.helpers since Handlebars will do this merge automatically at render time.

data : data,
helpers : helpers,
Expand All @@ -190,11 +188,19 @@ ExpressHandlebars.prototype.render = function (filePath, context, options) {
};

ExpressHandlebars.prototype.renderView = function (viewPath, options, callback) {
var context = options,
data = options.data;
options || (options = {});

var context = options;
var data = options.data;

var helpers = utils.extend({},
this.handlebars.helpers, this.helpers, options.helpers);
var helpers = utils.extend({}, this.helpers, options.helpers);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the merge of this.handlebars.helpers since Handlebars will do this merge automatically at render time.

var partials = Promise.all([
this.getPartials({cache: options.cache}),
Promise.resolve(options.partials)
]).then(function (partials) {
return utils.extend.apply(null, [{}].concat(partials));
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used fn.apply() here in case future code adds more sources the merging code won't have to change.

});

// Pluck-out ExpressHandlebars-specific options.
options = {
Expand All @@ -207,7 +213,7 @@ ExpressHandlebars.prototype.renderView = function (viewPath, options, callback)
utils.extend(options, {
data : data,
helpers : helpers,
partials: this.getPartials(options)
partials: partials
});

this.render(viewPath, context, options)
Expand Down