11

Using the Jade template engine, I want the ability to optionally declare classes to body tags, in templates that extends another.

layout.jade

!!! 5
html(lang='en')
head
    block title
    meta(charset='utf-8')
    link(rel='stylesheet', href='/stylesheets/style.css')
    block addhead

block body
    div#wrap
        header
            div#header_content
                p
                    a(href='/')
                        img(src='/images/logo.png', alt='Template-e-o')
            div.hr.red
        br(style='clear:both;')

        div#content
            block content
            br(style='clear:both;')

index.jade

extends layout

block title
    title bidi

block body
    body.index

block content
    p ’ello govna.

This is not working.

2
  • Can you post a more complete code example of what you tried and didn't work? Do you use an "extends layout" statement in your index.jade? It would help to see the whole .jade files. And just a "syntax sugar" hint, you can use body.index instead of (class='index').
    – mna
    Feb 9, 2012 at 15:49
  • Yea, thanks. Is this better? The template inheritance is working, it’s just how to optionally give the body tag a class, that I’m trying to figure out.
    – Frode
    Feb 9, 2012 at 17:47

3 Answers 3

38

Jade supports ruby like string interpretation

In your layout

- var bodyClass = '';
block vars
body(class='#{bodyClass}')
  block content

In your view files

extends layout
block vars
  - var bodyClass = 'my-page';
block content
  p Hello World!
3
  • 3
    This one works great if you're rendering static files from command-line (e.g. with Grunt) and can't easily pass in the variables dynamically.
    – Jed
    Sep 7, 2013 at 14:02
  • Why wouldn't you put the - var bodyClass inside the block vars in the first bit, out of interest? Mar 12, 2015 at 11:51
  • 2
    In my side, I had to define bodyClass like body(class=bodyClass) to make it work. Nov 18, 2017 at 7:43
6

You can manipulate variables in parent scope, so this solution is also possible:

In your layout

- var bodyClasses = [];
block beforeBody

body(class=bodyClasses)
  block content

In your view files

extends layout
block beforeBody
  - bodyClasses.push('my-page')
block content
  p Hello World!

Doing so lets you add multiple classes in different levels of your layout.

4

Ok, the block body statement is just a block with the name body, so in your layout you don't have a body tag included, it must be added in index.jade (which you try to do). However, in your index.jade, you replace the block content with only body.index (which, I guess - since I'm not on my dev computer and can't try it out right now - renders OK but takes out the whole content, you end up with just an empty body, although it should have the class='index' attribute).

What you could try is this (in index.jade):

block prepend body
    body.index

But I'm not sure if the rest of your layout.jade (div#wrap) will render correctly under body (I doubt it).

What I would recommend, if it's really just a matter of adding a conditional class on the body, is something like this in layout.jade (instead of block body):

body(class=myClassLocalVar)

Where myClassLocalVar is specified in your .render call (res.render('index', {myClassLocalVar: 'theClass'});). myClassLocalVar can even be an array, and jade will assign all the classes in the array to your body.

1
  • You’re right, using “block prepend body … body.index”, the body content is not rendering the way I want it to, but your solution does. Thanks a lot!
    – Frode
    Feb 9, 2012 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.