express icon indicating copy to clipboard operation
express copied to clipboard

Unable to set default directory for layouts with express-handlebars

Open max-fabertech opened this issue 11 years ago • 2 comments

Hello, I am unable to set default views directory.

OS: Ubuntu 15.04 Node: 4.0.0 I am in app directory (...). When running node server/server.js, it ignores app.set('views', path.join(__dirname, 'views')); from server.js and tries to load .../views/layouts/main.handlebars instead .../server/views/layouts/main.handlebars

.
└── server
    ├── server.js
    └── views
        ├── home.handlebars
        └── layouts
            └── main.handlebars

server.js

'use strict';

var path = require('path');
var express = require('express');
var exphbs  = require('express-handlebars');
var app = express();

// Rendering engine setup
app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.set('views', path.join(__dirname, 'views'));
console.log(path.join(__dirname, 'views'));

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

app.listen(8888);

EDIT: it works when i am in (.../server/ directory) and node server.js, it is wierd, becouse it should get right path from app.locals.setting.views variable.

max-fabertech avatar Sep 27 '15 16:09 max-fabertech

Hi! That modules does not seem to honor the Express setting when looking for the layouts directory. It looks like you have to tell express-handlebars where your layouts directory is yourself. Their documentation on this can be found at https://www.npmjs.com/package/express-handlebars#layouts

dougwilson avatar Sep 27 '15 17:09 dougwilson

It makes sense that the module lets you pick a layout directory, but it would be neat if they defaulted to "the layout folder inside the configured Express views directory". Right now, that module defaults to "views/layouts", which happens to be the layouts folder in the default Express views folder, but of course only if you don't change Express views setting :)

dougwilson avatar Sep 27 '15 18:09 dougwilson