ericf / express-handlebars

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

Default `layout` and `partial` folders to `views` configuration. #134

Open dnutels opened 9 years ago

dnutels commented 9 years ago

Hi.

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

// setup code snipped

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

var app = express();

app.set('views', PATH.resolve(__dirname, 'templates/layouts'));
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 templates is (and it is in the root folder, the same with the server.js above):

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

produces this (abridged):

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

The reason for that is this:

https://github.com/ericf/express-handlebars/blob/master/lib/express-handlebars.js#L27

where the path to layout is expected to be views/layouts without taking into consideration the actual placement of the views folder as configured by this:

app.set('views', __dirname + '/templates');

Current way to resolve such issues is to add an additional configuration using layoutsDir setting:

app.engine('hbs', HBS({
            extname:'hbs', 
            defaultLayout:'main.hbs', 
            layoutsDir: PATH.resolve(__dirname, 'templates/layouts')
        }));

I think the default shouldn't use views hard-coded, rather use app.get('views') in order to resolve the layouts and partials placement, while, perhaps, defaulting on layouts and partials folders' names.

I'd do PR, but I am a little stressed on time.

samuelfine commented 8 years ago

Looks like this is addressed by issue #147 and PR #148.