89

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

Flip
  • 6,233
  • 7
  • 46
  • 75
Thillai Narayanan
  • 4,766
  • 5
  • 32
  • 39

4 Answers4

138

HAML is indentation based , and the parser can be tricky.You don't need to use "- end" in Haml. Use indentation instead.In Haml,a block begins whenever the indentation is increased after a Ruby evaluation command. It ends when the indentation decreases.Sample if else block as follows.

- if condition
  = something
- else
  = something_else

A practical example

- if current_user
  = link_to 'Logout', logout_path
- else
  = link_to 'Login', login_path

Edit : If you just want to use if condition then

 - if current_user
  = link_to 'Logout', logout_path
bilash.saha
  • 7,226
  • 2
  • 35
  • 40
  • But how to use only if modifier – Thillai Narayanan Nov 08 '11 at 08:33
  • just use if like i have edited in the answer.no need to have end.The indentation will do the rest. – bilash.saha Nov 08 '11 at 08:42
  • An answer to your practical question: link_to_if http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_if – Jeffrey LeCours Oct 09 '13 at 02:28
  • I wanted to have another non output statement inside the if like - if current_user - i = 5 and was wondering why it wasn't working though I've indented it! Finally realised the - has to be indented :) – msanjay Aug 19 '14 at 08:57
  • 1
    How to add `or` operator to the condition? – Joe Hany Apr 13 '16 at 17:22
  • Only thing confusing me about this answer, is how to have multiple lines in the condition statement with just a single '='. There doesn't seem any point to using the = if indenting will do it – PandaWood Jul 11 '16 at 14:18
20

In haml two operators are used for ruby code.

  • = is used for ruby code which is evaluated and gets inserted into document.

Example:

= form_for @user  
  • - is used for ruby code which is evaluated and NOT get inserted into document.

Example:

- if @user.signed_in?  
  = "Hi"  
- else  
  = "Please sign in!"
Anne
  • 26,765
  • 9
  • 65
  • 71
Pratik Ganvir
  • 301
  • 2
  • 4
  • 1
    Please note you do not really need `=` if you want to output plain string like `Hi` or `Please sign in`. `=` is only necessary for evaluating Ruby, like `= "Hi, #{@user.name}"` – khustochka Sep 20 '12 at 13:40
9

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? %>
  <li><%= link_to "Sign out", sign_out_path %></li>
<% else %>
  <li><%= link_to "Sign in", sign_in_path %></li>
<% end %>
Michelle Tilley
  • 157,729
  • 40
  • 374
  • 311
2

If you want to put condition inside your tag

%section{:class => "#{'new-class' if controller.action_name == 'index'}"}

UPDATE

Here is another variation

%nav(class="navbar"){class: content_for?(:navbar_class) ? yield(:navbar_class) : nil}
Anton S.
  • 969
  • 1
  • 11
  • 29