How to write if condition in Haml

How to write a if condition, if else condition in haml for a ruby on rails application.

How to write this in HAML [closed]

How would I write the following using HAML? if @students.each do |student| # render a student end.empty? # @students was empty end

How to write this in haml?

What is that i should write to convert this to haml When I tried like this %p We prefer questions that can be %b answered . not just discussed. I got Illegal element: classes and ids must have values



How to write a HAML custom filter?

I have written a simple custom filter in HAML but I cannot get my Rails app to find it. The filter looks like this: # lib/haml/filters/gfmarkdown.rb module Haml::Filters::Gfmarkdown include Haml::Filt

How to write variable in haml

I’m trying to use haml on RoR. I faced some problems in haml. My sample code is below. ERB: <p> <div class=hello> <%= @sample.val %> <%= @hogehoge.val %> </div> </p&

How to write a parser for haml-like languages?

I want to write a parser and converter of haml-like languages, to parse them, and convert them into html content. I found people usually use regular-expression to do this, but we have to write a lot o

How to write condition in XML Schema?

Is it possible to write condition in XML schema? I need to write restriction for start and end time. So that difference between start and end time must be greater than 1:00 hour. I am not sure how to

How to write a complicated condition

Condition constructions are easy to write when you have a simple condition and a possibly complicated body: if simple_condition_expressed_in_one_liner complicated_body_that_may_be_long complicated_bod

Include? condition in haml view

I try to make something like this in haml view : %li{:class => @taxon and ([@taxon] + @taxon.ancestors).include?(taxon) : current} what the correct syntax ?

How to write the following HTML in HAML

I am working on converting some HTML to HAML. I am attempting to output the following in a Ruby on Rails form: <p>Date: <input type=text id=datepicker></p> What’s the best way t

How to convert Erb 2 Haml

I have write several erb template in views, how can I convert erb to haml automatically ? But not convert then hand by hand.

Answers

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 %>

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

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!"

I just had an Haml::SyntaxError at /de/bla – Got “else” with no preceding “if” thrown by the following code, which left me wondering for some time:

- if @user.avatar 
  .avatar{ style: "background-image: url(#{image_path @user.avatar})" }
/ else # this is a comment in HAML 
- else
  .avatar  
    .initials #{@user.initials}

Seems as if comments between if and else aren’t accepted by Haml.

Here are two links:

  1. https://html2haml.herokuapp.com/
  2. http://haml.info/tutorial.html

The first one is a converter- you can enter html or erb snippets and you get haml!