开发者

conditional blocks in haml

开发者 https://www.devze.com 2023-01-16 17:45 出处:网络
in ruby you can do conditional block like so block do |n| puts n end if foo == bar which would translate into erb as

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 %>
&开发者_开发技巧lt;% end if foo == bar %>

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


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

Haml does allow end in this circumstance.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消