1

I have theses two line in erb

<body class="<%= yield (:body_class) %>">
<% content_for :body_class, "my_class" %>

I have tried

- content_for :body_class do
  my_class

For above HAML conversion I'm not sure - correct or not ! and not able to figure out HAML conversion for

<body class="<%= yield (:body_class) %>">

Any help would be appreciated

Chandrakant
  • 1,971
  • 1
  • 14
  • 33

2 Answers2

1

You could do:

%body{ class: "#{yield (:body_class)}" }

and

- content_for :body_class do
    my_class
vthomas2007
  • 153
  • 6
0

There's a gem for that!

erb2haml (see details on Github) .

This gem will give you two commands which will allow you to:

  1. convert erb to haml

  2. convert and replace erb with haml

To keep the original ERB files you can run:

rake haml:convert_erbs

To discard the original ERB files after they have been converted to haml you can run:

rake haml:replace_erbs

There's also a gem called html2haml which works better if you aren't within a Rails application and/or you only want to convert specific files.

This gem give you the following code after running the html2haml command on your erb file:

%body{:class => "#{yield (:body_class)}"}
  - content_for :body_class, "my_class"
Andrew Hendrie
  • 6,205
  • 4
  • 40
  • 71
  • Always be careful with this type of thing - like any "translator", it might be correct only 60 / 70% of the time – Richard Peck Feb 06 '16 at 12:25
  • Yes it's correct, just saying that when you advocate using a translator, you have to remember to recommend leaning the code etc – Richard Peck Feb 06 '16 at 13:00
  • if you can provide some data that proves that this is only correct 60-70% of the time then I'll be quite surprised. Seems that quite a few people agree with me that this gem works quite well: http://stackoverflow.com/questions/2242699/convert-existing-html-erb-to-haml – Andrew Hendrie Feb 06 '16 at 13:01
  • better yet - if you can find a case where it doesn't convert .erb correctly you could create a pull request, write a test, write some code, and fix the edge case in order to improve the gem. – Andrew Hendrie Feb 06 '16 at 13:21