I have a rails project, the views only consist with HTML.ERB files, my client wants to convert ERB to HAML. I have too many views file. It's taking a huge amount of time to convert file by file. So that any simply way I can convert HTML to haml? I installed haml plugin under my project.
9 Answers
You can use from the command line html2haml
html2haml your_erb_file new_haml_file
If you want to convert all your files in one go, look at this article : http://shifteleven.com/articles/2008/06/08/converting-erb-to-haml-snippet

- 93,410
- 97
- 333
- 497

- 5,165
- 6
- 35
- 50
-
3According to the [latest html2haml source code](https://github.com/haml/html2haml), html2haml "is in the process of being extracted from the haml gem." – David J. Jun 15 '12 at 14:06
-
3now a [separate gem](https://github.com/haml/html2haml). looks like the current build isn't passing too. – Eric H. Feb 26 '14 at 15:49
-
I'm not too sure what the implications of the above two comments are supposed to be. I decided to give the command line `html2haml` a shot and it seems to work fine. – Jason Swett Jul 11 '15 at 18:38
There you go: http://html2haml.heroku.com/
EDIT: Moved to https://html2haml.herokuapp.com/

- 16,796
- 9
- 72
- 97

- 24,303
- 17
- 59
- 90
-
-
13This is not a 'bad' answer, but it should not be accepted as the best answer. The command line tool (below) is better. – David J. Jun 15 '12 at 13:50
-
2Please read the post, he said he didn't want to convert each individual file. – JackHasaKeyboard Dec 31 '15 at 01:37
A more user-friendly alternative to the selected answer.

- 8,201
- 10
- 53
- 90
-
User-friendly for small conversions, but a giant pain for doing a full app... – iconoclast May 13 '20 at 03:30
On the haml-rails git page, it provides the cli command to do convert all erb to haml right in your project.
add gem "haml-rails"
to your Gemfile
run: rake haml:erb2haml

- 1,463
- 1
- 15
- 19
very simple
in your Gemfile
add
gem "erb2haml", :group => :development
then run bundle install
for Converting *.erb
to *.haml
keeping original files do:
rake haml:convert_erbs
for Converting *.erb
to *.haml
replacing original files do:
rake haml:replace_erbs
it'll search all the erb
files in project and convert to haml
.
For shorthand: use on-line converter

- 6,799
- 3
- 42
- 47
EDIT: html2haml does work as advertised, however you must use version obtained from the current master branch of the haml github repoistory.
The version of html2haml included with the haml gem currently available from rubygems is no good. This is the version you will get if you were to do gem install haml
right now. Using the version supplied with the gem will result in invalid haml, as it cannot process ruby properly.

- 23,435
- 3
- 57
- 68
-
1Just wanted to add that as of now (2011/7/21), the haml gem's html2haml works. However, you do have to `gem install hpricot` and `gem install ruby_parser` on top of haml. If you try to do an erb->haml conversion, it'll prompt you to install these gems. – Eric Hu Jul 21 '11 at 21:27
html2haml is now in the html2haml gem, so you can use:
$ gem install html2haml
$ html2haml path/to/yourfile.html path/to/yourfile.haml

- 3,822
- 3
- 33
- 34
-
the application-wide answer is what is needed, not individual files. no one wants to type out individual files. it's only one step above doing it manually. an application-wide task will handle individual files. – ahnbizcad Jul 27 '14 at 10:45
Way late to the game here, but this post still flies high in the Google when searching for similar solutions.
Install the html2haml
gem, pop into your app/views directory and give this a try:
find ./ -name '*.erb' -exec html2haml -e {} {}.haml \;
find ./ -name "*.erb.haml" -exec sh -c 'mv "$1" "${1%.erb.haml}.haml"' _ {} \;
find ./ -name '*.erb' -exec rm {} \;
The flaw in this solution is that it doesn't retain revision history from your old .erb files to your new .haml files. But at times where that revision history of those view files isn't a big deal, this solution has served me pretty well.
Also, be sure to watch for any errors in the html2haml line before you delete the old .erb files.