Open sundarj opened 9 years ago
I'm actually wondering if getting the view's "name" (the value passed to res.render()
) is unknowable when the app is using multiple views
dirs.
I'm not sure what should be considered the best match. We can use path.relative()
on each of the views
dirs to get back possible values to use as the view's "name", and that will work on Windows, but what should be the criteria to pick a winner?
Ah, so if there happen to be multiple .handlebars files named the same thing in different views? Perhaps an error message asking one of them to be renamed, or giving each a unique name, e.g. res.render('index-0')
, would work?
If you have the following views dirs:
/path/to/views/
/path/to/views/pages/
/path/to/other/views/
And the view that's being rendered is at: /path/to/views/pages/home.hbs
Using file.indexOf(dir) === 0
you'd get the following results:
true
true
false
Using this._getTemplateName(dir, file)
on the true
results above, you'd get the possible view names:
pages/home
home
So the question is which one of these do you pick? Is it simply the shortest one? I think that in this case it would be reasonable to pick #2
.
Yeah, that's why I used the strip-filename regex. With those three views dirs, you'd only get one possibility: /path/to/views/pages/
. But I do agree it'd be reasonable to pick #2
, and it could be overridden by specifiying pages/home
, I suppose?
and it could be overridden by specifiying pages/home, I suppose?
Nope it can't because no matter what's specified to res.render()
, Express always passes the full path to the view file into the template engines.
Is this fixed? If so, which version do I need?
Taking a step back: much of this would be resolved if view.render()
simply included the name of the view, right? View.prototype.render
in /express/lib/view.js has access to this.name
, but doesn't pass it along to this.engine()
. If we knew the view name, we'd know—in the context of @ericf's example—that the view is, in fact, pages/home
and not home
.
Is there any update/decision on this? Restricting to a single views folder is really quite limiting.
Just ran into this issue today as well, would also like to know if there are any updates.
I really need this feature too. @ericf are you planning to merge this? are you waiting for something?
Also really need this feature.
I noticed after going through express-handlebars.js that nothing seems to need the view
property. By removing that property and commenting out this line: view = this._getTemplateName(path.relative(viewsPath, viewPath));
, then express-handlebars seems to work just fine with an array of view paths.
My question is: Is there any reason I shouldn't be pulling out the view property of the options object?
Any updates on this? Did anyone cloned this repo and merged this...?
Development has moved to https://github.com/express-handlebars/express-handlebars/ if you would like to create a PR on that repo we could add this.
Fixes #124.
I've added a fix based on your help, and now when views is an array, res.render checks both folders (I've tested). I've tried to match the existing codebase/style as closely as possible, but there's probably still a few things you can fix (like the
_getView
comment).Hope it's good enough!