我想要的是"if"中的什么,以及"else"中包含#main-block的内容.
- if @transparency
#content-inner{:style => "background: url(../../../images/illustrations/" + @transparency + ") no-repeat 88% 50%"}
- else
#content-inner
#main-block
Run Code Online (Sandbox Code Playgroud)
目前发生的事情是,如果@transparency已定义,#main-block则不会嵌套在内部#content-inner.
tad*_*man 22
您可以使用三元运算符有条件地应用样式属性:
#content-inner{ :style => @transparency ? "background: url(../../../images/illustrations/" + @transparency + ") no-repeat 88% 50%" : '' }
#main-block
Run Code Online (Sandbox Code Playgroud)
对于更复杂的排列,例如操作多个散列属性,最好使用辅助方法,或将公共内容移动到部分.