ericf / express-handlebars

A Handlebars view engine for Express which doesn't suck.
BSD 3-Clause "New" or "Revised" License
2.31k stars 384 forks source link

Location script is executed seems to matter #43

Closed stevetweeddale closed 10 years ago

stevetweeddale commented 10 years ago

When my node script is executed from a different directory to that which it lives, this library can't locate the templates. I'm still new to nodejs, so I don't know if this is just a given that you'll always start the node process from the directory it lives?

This can be reproduced with this repo itself. If rather than navigating to examples/basic and running the example from there, you instead fire if off from somewhere else, you get the error Error: Failed to lookup view "home".

The reason I run into this is because I have a script that starts the process without cding first. Obviously I can just tweak that, but I was using a different template engine before that had no problems - so it led to a fair bit of head scratching when making the change, so I wonder if it's a bug?

ericf commented 10 years ago

The defaults for the partialsDir and layoutsDir are configurable: https://github.com/ericf/express3-handlebars#configuration-and-defaults

In your app you could use the process.cwd() API to change the current working dir and set these options accordingly. Also you'll want to set the "views" Express option to your views dir.

rikkertkoppes commented 10 years ago

That would be something like

app.engine('handlebars', exphbs({
    layoutsDir: path.resolve(__dirname,'views/layouts'),
    partialsDir: path.resolve(__dirname,'views/partials'),
    defaultLayout: 'main'
}));
app.set('views',path.resolve(__dirname,'views'));

do not forget to set the views directory itself. It is a setting of express (last line)