user138095
user138095

Reputation:

conditional blocks in haml

in ruby you can do conditional block like so

block do |n|
  puts n
end if foo == bar

which would translate into erb as

<% block do |n| %>
  <%= n %>
<% end if foo == bar %>

is there a way to achieve this in haml other than wrapping the block in a condition?

Upvotes: 2

Views: 1689

Answers (2)

David J.
David J.

Reputation: 32705

Please put the if up front -- it makes it more readable:

- if foo == bar
  - block do |n|
    = n

And, if this occurs frequently, consider writing a custom Haml helper.

(I mentioned the GitHub Ruby Style guide above in a comment. If you search Google for 'ruby do end chaining' you'll see many style guides that recommend using that construct.)

Upvotes: 0

Natalie Weizenbaum
Natalie Weizenbaum

Reputation: 5964

- block do |n|
  = n
- end if foo == bar

Haml does allow end in this circumstance.

Upvotes: 7

Related Questions