dsounded
dsounded

Reputation: 723

Haml nested blocks

I am newbie in HAML. So what about this in HAML ?

    -if @link.is_active
      %a
    -else
       #custom-div
         .custom-class

So I want to see if some condition is true

    <a><div id = "custom-div"><div class = "custom-class"></div></div></a>

And if it's false:

    <div id = "custom-div"><div class = "custom-class"></div></div>

Without copying of blocks, I mean not this:

   -if @link.is_active
     %a
       #custom-div
         .custom-class
    -else
       #custom-div
         .custom-class

Any proposes ?

Upvotes: 1

Views: 346

Answers (2)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Depending on how complicated #custom-div .#custom-class really is you could use link_to_if:

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_if

= link_to_if @link.is_active, "html safe linkable text here", "url to link to"

Upvotes: 1

matt
matt

Reputation: 79723

Haml 4.1.0.beta.1 has a haml_tag_if helper. You can use it like this:

- haml_tag_if @link.is_active, :a do
  #custom-div
     .custom-class

If you can’t use 4.1.0 you could add the helper yourself, it is fairly simple. This should work:

def haml_tag_if(condition, *tag)
  if condition
    haml_tag(*tag){ yield }
  else
    yield
  end
end

Upvotes: 1

Related Questions