Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jade layout can not be found in subdirectory

My settings:

My directory structrue looks like this:

/views/
   |-- index.jade
   |-- layout.jade
   /account/
      |-- login.jade
      |-- register.jade

I have configured my view engine as follows:

app.set('view engine', 'jade');

Edit: I also have

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

My controllers look like:

app.get('/login', redirectIfLogined, function(req, res){
    res.render('account/login', { title: "Login", redir: req.query.redir});
});

app.get('/register', redirectIfLogined, function(req, res) {
    res.render('account/register', { title: "Register"});
});

My issue

With settings as described I am getting a 500 error:

500 Error: failed to locate view "layout"
at Function.compile (D:\dev\Azznomad\express-template\node_modules\express\lib\view.js:58:15)
at ServerResponse._render (D:\dev\Azznomad\express-template\node_modules\express\lib\view.js:414:18)
at ServerResponse.render (D:\dev\Azznomad\express-template\node_modules\express\lib\view.js:315:17)
at ServerResponse._render (D:\dev\Azznomad\express-template\node_modules\express\lib\view.js:429:10)
at ServerResponse.render (D:\dev\Azznomad\express-template\node_modules\express\lib\view.js:315:17)
at D:\dev\Azznomad\express-template\controllers\accountController.js:6:13
at callbacks (D:\dev\Azznomad\express-template\node_modules\express\lib\router\index.js:272:11)
at D:\dev\Azznomad\express-template\modules\middleware\account.js:27:9
at callbacks (D:\dev\Azznomad\express-template\node_modules\express\lib\router\index.js:272:11)
at param (D:\dev\Azznomad\express-template\node_modules\express\lib\router\index.js:246:11)

As soon as I am copying my layout.jade file into the /account/ folder it works perfectly. Unfortunately this would make no sense since it forced me to have redundant layout files.

What am I missing?


Btw. this code snipplets are from AzzNomad's express/mongoose template which is very helpful in order to learn express with mongoose.

like image 641
Sven Avatar asked May 06 '12 17:05

Sven


2 Answers

You need to put

extends ../layout

in your view under accounts since layout is not in the same folder.

like image 117
Hadi Hariri Avatar answered Sep 20 '22 15:09

Hadi Hariri


You should also set views path, since it is in another directory besides application script.

app.set('views', __dirname + '/views');
like image 1
Mustafa Avatar answered Sep 21 '22 15:09

Mustafa