如果在玉中有其他一行与儿童元素的变化

时间:2017-07-15 21:35:16

标签: express pug

我想让我的代码更干,并且正在努力解决如何在if / else语句中获取单行,同时将子div保留在if / else语句的结果下。

我尝试过花括号,然而,我无法让它发挥作用。 谢谢!

    if (viewOptions.visible == 'true')
        .myDiv1        
    else
        .myDiv1(style="display:none")
           //either way children get added to myDiv1

1 个答案:

答案 0 :(得分:0)

我使用的解决方案是使用来自pug文档的mixin和&attributes(attributes)https://pugjs.org/language/mixins.html

if (viewOptions.dataVisibility == 'true')
      +propertySection('My input', "enter a number", source._data.length, 'myInputName')            
    else 
      +propertySection('My input', "enter a number", source._data.length, 'myInputName')(style="display:none")

mixin propertySection(header, label, initVal, inputName)
  .myDiv1&attributes(attributes)
    .myHeader= header
    .myLabel= label
    input(value='#{initVal}' name="#{inputName}")