diff --git a/lib/express-handlebars.js b/lib/express-handlebars.js index 93a8590..3a12a38 100644 --- a/lib/express-handlebars.js +++ b/lib/express-handlebars.js @@ -189,7 +189,7 @@ ExpressHandlebars.prototype.renderView = function (viewPath, options, callback) var view; var viewsPath = options.settings && options.settings.views; if (viewsPath) { - view = this._getTemplateName(path.relative(viewsPath, viewPath)); + view = this._getView(viewsPath, viewPath); } // Merge render-level and instance-level helpers together. @@ -327,6 +327,24 @@ ExpressHandlebars.prototype._getTemplateName = function (filePath, namespace) { return name; }; +ExpressHandlebars.prototype._getView = function(viewsPath, viewPath) { + if (!Array.isArray(viewsPath)) { + return this._getTemplateName(path.relative(viewsPath, viewPath)); + } + + var self = this; + var stripFilename = /[\/\\][^\/\\]+?$/; + + // Stripping filename (rather than indexOf) in case there are + // multiple views folders in the same parent directory + + viewsPath.forEach(function(viewDir) { + if (viewDir === path.normalize(viewPath).replace(stripFilename, '')) { + return self._getTemplateName(path.relative(viewDir, viewPath)); + } + }); +} + ExpressHandlebars.prototype._resolveLayoutPath = function (layoutPath) { if (!layoutPath) { return null;