Escaping if/else in Jade template

Posted by Collin Estes on Stack Overflow See other posts from Stack Overflow or by Collin Estes
Published on 2014-08-18T14:39:25Z Indexed on 2014/08/18 16:22 UTC
Read the original article Hit count: 199

Filed under:

So I have a simply block of html in my jade template with a loop:

  each dude, i in dudes
    div(class='someClass')
      div.otherStuff
        span.someContent #{name}

I want to apply a data attribute to my top most div when a certain looping condition is met, in this case the first dude, I set it up like this

  each dude, i in dudes
     if i == 0
       div(class='someClass, data-someAttr='first dude')
     else
       div(class='someClass')
          div.otherstuff
            span.someContent

Setup like this it causes the div.otherStuff div and the span.someContent to only display on the else condition. I've moved the tab spaces around on those items but I can't get it escape that else and give the div.otherStuff and span.someContent to both the first dude and the subsequent dudes after that. I've also tried setting that data attr to a variable and trying to apply it that way but without any success.

What I end up doing to get around it is this:

  each dude, i in dudes
     if i == 0
       div(class='someClass, data-someAttr='first dude')
          div.otherstuff
          span.someContent
     else
       div(class='someClass')
          div.otherstuff
            span.someContent

How do I escape that if/else so I dont have to duplicate the div.otherStuff and span.someContent?

© Stack Overflow or respective owner

Related posts about jade