条件haml – if else嵌套

我想要的是“if”中的什么,以及“else”中包含#main-block的内容。

- if @transparency #content-inner{:style => "background: url(../../../images/illustrations/" + @transparency + ") no-repeat 88% 50%"} - else #content-inner #main-block 

目前发生的情况是,如果定义了@transparency#main-block不会嵌套在#content-inner

您可以使用三元运算符有条件地应用样式属性:

 #content-inner{ :style => @transparency ? "background: url(../../../images/illustrations/" + @transparency + ") no-repeat 88% 50%" : '' } #main-block 

对于更复杂的排列,例如操作多个散列属性,最好使用辅助方法,或将公共内容移动到部分。