Jade Inline Conditional

后端 未结 7 1594
你的背包
你的背包 2021-01-31 15:43

I\'m trying to make everything apart from the first element in an array have a CSS class using the Jade templating engine.

I was hoping I could do it like this, but no l

7条回答
  •  情歌与酒
    2021-01-31 16:30

    I prefer to use simple functions to check any complex conditions. It's works perfect and fast, you shouldn't write long lines in template. Can replace this

    - each sense, i in entry.senses
      - var klass = (i === 0 ? 'span13' : 'span13 offset3')
      div(class=klass)
        ... a tonne of subsequent stuff
    

    to this

    -function resultClass(condition)
     -if (condition===0)
      -return 'span13'
     -else if (condition===1)
      -return 'span13 offset3'
     -else if (condition===2) //-any other cases can be implemented
      -return 'span13 offset3'
     -else
      -return 'span13 offset3'
    
    - each sense, i in entry.senses
      div(class=resultClass(i))
        ... a tonne of subsequent stuff
    

    Hope it helps and the idea is clear to understand.

    Also it's good practice to move all functions in include file and share it between different templates, but it's another question

提交回复
热议问题