Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline unbuffered code in jade templates

Tags:

node.js

pug

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?

like image 428
iopq Avatar asked Oct 25 '13 23:10

iopq


2 Answers

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"
    }]
  }]
like image 81
kadrian Avatar answered Oct 04 '22 00:10

kadrian


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.

like image 26
SomeKittens Avatar answered Oct 04 '22 01:10

SomeKittens