jade: if statements and nesting

前端 未结 3 1614
孤街浪徒
孤街浪徒 2021-02-07 07:41

Concider this pseudo-ish server side code

if(isFixed) {
  
} else {
}

Inner element

3条回答
  •  星月不相逢
    2021-02-07 08:12

    You need to separate control flow from the template. Try this:

    divClass = null
    
    if isFixed
       divClass = "fixed"
    
    div(class=divClass)
       p inner element
    

    Which in turn might suggest factoring out the "isFixed" parameter into the actual divClass parameter to be passed on. But that depends on your remaining code/template of course.


    As an alternative, try a mixin:

    mixin content
      p inner element
    
    if (isFixed)
      div(class="test")
        mixin content
    else
      div(class="other")
        mixin content
    

提交回复
热议问题