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

Unable to set default directory for views #147

Closed jacob-faber closed 8 years ago

jacob-faber commented 8 years ago

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.

swarajgiri commented 8 years ago

@camabeh - path.join(__dirname, 'views')) searches for server/views/<your-file-here>. But the views are not inside server folder. Update the path of views to use the correct path. app.set('views', __dirname + '/../views');

jacob-faber commented 8 years ago

@swarajgiri I don't get it. I think it should respect app.locals.settings.views and set layouts/ and partials/ relative to new views path. It looked in project's root folder for views/partials and views/layouts

Here I fixed it. https://github.com/ericf/express-handlebars/pull/148

lmaran commented 8 years ago

@camabeh - I have exactly the same issue. I modified (temporary) the code in the library according to your PR changes and confirm that it solves the problem. Until your PR is merged I choose to set layoutsDir and partialsDir in engine factory:

app.set('views', config.root + '/server/views');

var exphbs = require('express-handlebars');
app.engine('.hbs', exphbs({
        defaultLayout: 'main', 
        extname: '.hbs',
        layoutsDir:'server/views/layouts',
        partialsDir:'server/views/partials'
}));
app.set('view engine', '.hbs');
NBThiet commented 2 years ago

I have a problem: "Error: The partial header could not be found....."

does anyone know how to solve it?

nathandelgado-dev commented 2 years ago

Thanks Bro for this!!