Happy Haml :)

 Haml is a much cleaner way to write ruby executable with html. Unlike erb or rhtml, you need not have to give tags for every if/for loops, no need of <> tags, only remember haml works in indentations.
 You need to give 2 spaces of indentations under the main loop.

Like for example, you are looping through each element in an array, then for the code which goes inside the loop will look like this
-['a', 'b'].each do |letter|
  = letter

  1. gem install haml
  2. or in bundler, add gem 'haml'
  3. make a file inside the view/ directory with extension as .haml.html

remember haml works with indentation. 2 spaces 

  • - has to be added for ruby code which has not to be evaluated <% %>, like for, if, else
  • = for ruby code which has to be evaluated, similar to <%= %>
  • % for html tags, like th, td, h1 etc.
  • no tags are required in haml

%h1
  doel


The above two lines will create a html page, with headers as doel.

for label tag in html, in haml label will come under p tag
- form_for @user do |f|
  %div
    %p
      = f.text_field :name
      User name


And if you have all your codes in html.erb and would lke to convert that to Haml very quickly, you can try out the below link, it's easy :)
http://html2haml.heroku.com/
But remember to test your app with the new haml before pushing the changes..


...coming soon more and more on happy Haml


Post a Comment

Popular posts from this blog

Loved this campaign by Vicks India

Uninitialized constant Win32 (NameError)-cucumber