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 array as views #126

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/express-handlebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

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

Yeah, this is why we need a criteria for determine the best match. Should we go with, the file must exist in the dir, and have the shortest length to be the winner?


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;
Expand Down