Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conditional haml - if else nesting

Tags:

ruby

haml

What I want is both whats in "if" and whats in "else" to include #main-block.

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

What happens currently is, if @transparency is defined, #main-block is not nested inside #content-inner.

like image 668
Dr. Frankenstein Avatar asked Nov 16 '09 12:11

Dr. Frankenstein


1 Answers

You can use a ternary operator to conditionally apply the style attribute:

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

For a more complicated arrangement, for example manipulating multiple hash attributes, it's best to either use a helper method, or to move the common content to a partial.

like image 175
tadman Avatar answered Oct 24 '22 00:10

tadman