How to write if
and if
-else
statements in Haml for a Ruby on Rails application?
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 %>