How to write if-condition in Haml?

前端 未结 5 1424
长发绾君心
长发绾君心 2021-01-31 00:55

How to write if and if-else statements in Haml for a Ruby on Rails application?

5条回答
  •  野的像风
    2021-01-31 01:37

    In haml, use the - (dash) to indicate a line is Ruby code. Furthermore, indention level indicates block level. Combine the two for if/else statements.

    - if signed_in?
      %li= link_to "Sign out", sign_out_path
    - else
      %li= link_to "Sign in", sign_in_path
    

    is the same as the following code in ERB:

    <% if signed_in? %>
      
  • <%= link_to "Sign out", sign_out_path %>
  • <% else %>
  • <%= link_to "Sign in", sign_in_path %>
  • <% end %>

提交回复
热议问题