8

I am trying to write a template that renders a double indexed array. So I started writing this:

- var grid = [[1, 0, 1], [0, 1, 0]];
each row in grid
    each cell in row
        if cell
            span x
        else
            span o

but this is not how I want to write my array I want to write it like this:

- var grid = [[1, 0, 1],
              [0, 1, 0]];

This doesn't work because jade is already out of the inline javascript

- var grid = [[1, 0, 1],
             - [0, 1, 0]];

This doesn't work because jade considers those two incorrect lines instead of one line

How can I make it work?

2 Answers 2

9

Update: Multiline defs are now working for me using Jade 1.11.0. Even nested JSON now works like a charm.

-
  projects = [{
    title: "Project 1",
    classname: "project1",
    slides: [{
      title: "Slide 1"
      img: "images/hello.png"
    },{
      title: "Slide 2"
      img: "images/world.png"
    }]
  }, {
    title: "Project 2",
    classname: "project2",
    slides: [{
      title: "Slide 3"
      img: "images/fun.png"
    },{
      title: "Slide 4"
      img: "images/things.png"
    }]
  }]
0
1

EDIT: Yay, these are real! Go check out the other answer on how to pull this off.

Sadly, this is currently not possible in Jade. TJ (the maintainer) has stated that he does not care about this, but would welcome a feature request. https://github.com/visionmedia/jade/issues/796

Fortunately, you can declare the array in your JS file and pass it as a variable to Jade.

8
  • Ok. Could you give an example? If I out a variable into a js file, the variable is wrapped into a function definition during the compile process (I think it is a module then...). I am using brunch with jade. How can I directly include a js (or JSON file from jade)?
    – nerdoc
    Feb 7, 2015 at 16:58
  • @NerDoc that sort of thing (passing variables to Jade) is covered in the docs: jade-lang.com/reference Feb 7, 2015 at 17:46
  • Aaaaah. I must have overlooked, that includes does cover text includes - so if I understand correctly, I can use script \n include blah.js to have the variables directly in the current file, right?
    – nerdoc
    Feb 7, 2015 at 20:48
  • This does not work. when I include a file that contains var blah = 5;, I can not access the variable blah from within the parent file. I'm not fluent in Js (as you might think)... could you give me a hint here?
    – nerdoc
    Feb 7, 2015 at 21:37
  • @NerDoc I assume you are referring to including it within a script tag as in your previous comment? Then no - the variable would not be available to Jade at compile time. Instead it would be declared within the context of the browser. Jade is an amazing and beautiful templating language IMO but it can also be easy to get yourself into slightly confusing situations such as this.
    – Hal
    Mar 16, 2015 at 23:14

Your Answer

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

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