jade - using if statement

I want to get following if condition in jade.

each item,count in display
         if(count % 3 === 0)
          {
            ul.thumbnails
          } 
             li.span6 //nested under ul
               //more code nested under li

I googled searched a lot, but with no luck. Basically, I want to make a new list for every count which is divisible by 3

I tried this:

mixin display_list
   li
    //code


each item,count in display
    -if(count === 0 )
        ul.thumbnails 
            mixin display_list
    -else
            mixin display_list

It still doesn't work!

Upvotes: 0

Views: 3844

Answers (2)

Andreas Hultgren
Andreas Hultgren

Reputation: 14953

Since Jade forces you to indent stuff that is nested, I think the only way (not sure, but it's certainly the most straightforward) is to do it like this:

- var i = 0;
- while(i < display.length)
  ul.thumbnails
  - var k = i + 3
  - while(i < k && i < display.length) // Will repeat three times, unless display.length isn't large enough
    li.span6 //nested under ul
      //more code nested under li
    - i++

Assuming display is an array

(This answer has been updated, the former answer was completely wrong)

Update 2: Fixed that k could be greater than display.length

Upvotes: 1

I found a way but I don't think it is the correct way to do it.

a = [1,2,3,4,54,6,7,8,9,4,5]

each item, i in a
  if i % 2 == 0
    |<ul>

  li #{item}

  if (i+1) % 2 == 0
    |</ul>

Upvotes: 0

Related Questions