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

express-handlebars layouts not using "views" setting? #2675

Closed
dnutels opened this issue Jun 12, 2015 · 11 comments
Closed

express-handlebars layouts not using "views" setting? #2675

dnutels opened this issue Jun 12, 2015 · 11 comments
Assignees

Comments

@dnutels
Copy link

dnutels commented Jun 12, 2015

Hi.

I am a bit puzzled by this, as there are no reports, yet my various attempts came up with nothing.

Anyway - trying to relocate the views folder doesn't work.

This code (in server.js file in the root of the project):

var express = require('express');
var expressHbs = require('express-handlebars');

var app = express();

app.set('views', __dirname + '/views1');
app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs'}));
app.set('view engine', 'hbs');

app.get('/', function(req, res){
  res.render('index');
});

app.listen(8080);

where views1 is (and it is in the root folder, the same with the server.js above):

views1                         
└───layouts     
│        └───main.hbs
└───index.hbs   

produces this (abridged):

Error: ENOENT, open '<path-to-root-folder>/views/layouts/main.hbs'

Renaming the folder to views and removing app.set('views', ...); line makes this work properly.

I must be missing something, as this is too basic a functionality to not work.

P.S.

There were additional attempts revolving around using something like this instead (to make sure that the path is resolved properly and whatnot):

app.set('views', path.resolve(__dirname, 'views1'));

to no avail.

@dougwilson
Copy link
Contributor

Hi! Can you start your app with the following command, and paste what is printed on the command line here?

DEBUG=express:application node server.js

@dougwilson dougwilson self-assigned this Jun 12, 2015
@dnutels
Copy link
Author

dnutels commented Jun 12, 2015

Ehh...... probably won't do you any good, but...

  express:application compile etag weak +0ms
  express:application compile query parser extended +0ms
  express:application compile trust proxy false +0ms
  express:application booting in development mode +0ms
Listening on http://localhost:8080
GET / 500 44.299 ms - 148
Error: ENOENT, open '<path-to-root-folder>\views\layouts\main.hbs'
    at Error (native)

Note I removed the path and replaced it with <path-to-root-folder> due to various reasons.

EDIT: the port moved to 8080. Updated my earlier example as well.

@dougwilson
Copy link
Contributor

Thanks! I forgot that we are not actually printing all the settings in the debug output (bug! :) ) so that doesn't seem to help. I'll have to try it out, but in the end, the views module you are using, express-handlebars, is ultimately responsible for honoring that setting. If I have time to debug, I'll try to see where it's getting lost, but I suspect it's happening in the express-handlebars module.

@dnutels
Copy link
Author

dnutels commented Jun 12, 2015

I fail to see how is it responsible, since changing the name to views works just fine.

But thanks for trying it out.

@dougwilson
Copy link
Contributor

Gotcha. Yea, I'm not sure, but I will try to investigate later :) If you do fine the issue in Express, a PR would be welcome!

@dougwilson
Copy link
Contributor

I fail to see how is it responsible, since changing the name to views works just fine.

This is because we only resolve the index file in your example; your error is failing to resolve main.hbs, which the way that is resolved is done purely by express-handlebars module, as layouts are not a part of Express and implemented by the view engines (which is why you had to give that option to their constructor).

@dnutels
Copy link
Author

dnutels commented Jun 12, 2015

Ah, I see. Lemme try this with jade

@dougwilson
Copy link
Contributor

Looks like you need to specify the layoutsDir option, for express-handlebars to resolve layout files correctly (at least, that's what I'm getting from reading https://github.com/ericf/express-handlebars/tree/v2.0.1#layoutsdirviewslayouts):

app.engine('hbs', expressHbs({extname:'hbs', defaultLayout:'main.hbs', layoutsDir: __dirname + '/views1/layouts'}));

@dougwilson dougwilson changed the title Renaming views folder doesn't work. express-handlebars layouts not using "views" setting? Jun 12, 2015
@dnutels
Copy link
Author

dnutels commented Jun 12, 2015

Yep. My bad. 😵 ... what's the embarrassed emoji name?

@dougwilson
Copy link
Contributor

lol. I think it's U+1F633 FLUSHED FACE 😳

I'm going to close this, but it may be worth making a bug over at express-handlebars (or I guess even a PR), because we do provide what the views dir is to the view engine, so the module could default to expressViewsDir + '/layouts' instead of just 'views/layouts'.

@dnutels
Copy link
Author

dnutels commented Jun 12, 2015

Will move it over there, sure. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants