Skip to content

Commit

Permalink
Add computed view name to {{@ExpressHandlebars}}
Browse files Browse the repository at this point in the history
Fixes #101
  • Loading branch information
ericf committed Feb 13, 2015
1 parent 5b90ad9 commit 47697a9
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/express-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ ExpressHandlebars.prototype.getPartials = function (options) {
}, this);

return Promise.all(partialsDirs).then(function (dirs) {
var getPartialName = this._getPartialName.bind(this);
var getTemplateName = this._getTemplateName.bind(this);

return dirs.reduce(function (partials, dir) {
var templates = dir.templates;
var namespace = dir.namespace;
var filePaths = Object.keys(templates);

filePaths.forEach(function (filePath) {
var partialName = getPartialName(filePath, namespace);
var partialName = getTemplateName(filePath, namespace);
partials[partialName] = templates[filePath];
});

Expand Down Expand Up @@ -176,6 +176,15 @@ ExpressHandlebars.prototype.renderView = function (viewPath, options, callback)

var context = options;

// Express provides `settings.views` which is the path to the views dir that
// the developer set on the Express app. When this value exists, it's used
// to compute the view's name.
var view;
var viewsPath = options.settings && options.settings.views;
if (viewsPath) {
view = this._getTemplateName(path.relative(viewsPath, viewPath));
}

// Merge all sources of helpers together.
var helpers = utils.assign({},
this.handlebars.helpers, this.helpers, options.helpers);
Expand All @@ -184,6 +193,7 @@ ExpressHandlebars.prototype.renderView = function (viewPath, options, callback)
// rendering options.
options = {
cache : options.cache,
view : view,
layout: 'layout' in options ? options.layout : this.defaultLayout,

data : options.data,
Expand Down Expand Up @@ -291,7 +301,7 @@ ExpressHandlebars.prototype._getFile = function (filePath, options) {
});
};

ExpressHandlebars.prototype._getPartialName = function (filePath, namespace) {
ExpressHandlebars.prototype._getTemplateName = function (filePath, namespace) {
var extRegex = new RegExp(this.extname + '$');
var name = filePath.replace(extRegex, '');

Expand Down

0 comments on commit 47697a9

Please sign in to comment.