Adam Kowalski
Adam Kowalski

Reputation: 1

Node JS - error when extends layout in Jade

If I understood, I can move some parts of template to parent-template using when I use Jade?

I try to move link to some js files to head section, that I use only with one part of website, but I get an error.

My layout.jade:

!!!
html(lang='en')
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/box.css')
    block scripts
  body
    ..

my index.jade (just for try, it will be in other file)

extends layout

block scripts
  script(src='myscriptfile.js')
div#sortable
  ..

and some part of node server:

app.get('/', function(req, res){
  res.render('index', 
    {title: 'My title',
    images: images}
  );
});

I switch this option true/false

app.set('view options', { pretty: true });

but still get an error:

Express
500 ReferenceError: E:\projekt/views/index.jade:13 11| 12| > 13| 14| body is not defined

What am I doing wrong?

Upvotes: 0

Views: 2481

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123443

In the .. of your snippets, you're referencing a body variable that hasn't been defined. I'm guessing around line 13 of index.jade:

500 ReferenceError: E:\projekt/views/index.jade:13 11| 12| > 13| 14| ...

You'll either have to remove it from the view or define it in the view data:

res.render('index', 
    {title: 'My title',
    images: images,
    body: '...'}
);

Side note:

If you haven't upgraded to Express 3.x, you may want to ensure layouts are disabled:

app.set('view options', {
  layout: false,
  pretty: true
});

Inheritance and blocks are meant to replace layouts, not work with them. As stated in Migrating from 2.x to 3.x:

Removed... the concept of a "layout" (template engine specific now)

Upvotes: 2

Related Questions