Converting ERB to HAML snippet

I’m playing around with merb so I’m using merb-gen to create some basic scaffolding; however, I’m not using ERB, the default rendering engine, I want to use haml

Haml comes with a script, html2haml, which can take HTML with ERB and convert it nicely to haml. Seeing as I would have to run that command more than once, I have a little command snippet that I like to use to aid with the conversion.

$ find . -name '*erb' | \
xargs ruby -e 'ARGV.each { |i| puts "html2haml -r #{i} #{i.sub(/erb$/,"haml")}"}'

That will print out what the html2haml commands would be, it does *not* run the command. I do that step such that I can review what will be converted. If I like what’s going to be done, then I just run that command and pipe it into bash…like so

$ find . -name '*erb' | \
xargs ruby -e 'ARGV.each { |i| puts "html2haml -r #{i} #{i.sub(/erb$/,"haml")}"}' | \
bash

If you like it, it probably wouldn’t hurt storing that as a shell script somewhere :)

  • http://EmmanuelOga.WordPress.com Emmanuel Oga

    Nice trick, I like it :)

    Greets

  • http://prolificcoder.com Satyajit

    Very useful script, I would only want a way to remove the .erb files after conversion.
    I did a seperate rm `find . -name ‘*erb’` Is there a way to combine them other than say using &&

  • Sachin singh

    i m new to rails and haml please give me step by step tutorial how yo use this script to convert erb template to haml one and all in one step on windows xp.

    Please help me!

  • http://table14software.com Ben

    Nice trick! worked perfectly.

  • Jozef

    It works nice, but the problem is with any form tags. It isn’t formatted code, so you will get a error syntax error, unexpected kENSURE, expecting kEND .. maybe if you can use this online convertor http://html2haml.heroku.com/

  • http://www.postalrevolution.com Paul Russell

    Nice tip. One extra: Once you’re absolutely /sure/ you’ve converted your files successfully, you can execute the following to remove the erb files you no longer need.

    $ find . -name ‘*erb’ -delete

    P.S. Should technically be ‘html2haml -e’ these days rather than -r, as -r is deprecated.

  • Dale

    Thanks. This is exactly what I was looking for.

  • Pingback: » Converting ERB to HAML Structured Chaos

  • Alex

    cat > ~/erb2haml <<EOF
    #!/bin/bash
    for i in `find . -name '*.erb'` ; do
    html2haml $i `echo $i | sed 's/\.erb/\.haml/'`
    done
    find . -name '*.erb' -exec rm \{\} \;
    EOF
    chmod +x ~/erb2haml
    cd myrailsproject ; ~/erb2haml

  • Pingback: Code to Self: Converting ERB to HAML | Struktured Kaos

  • Pingback: Converting ERB to Slim | Debugging

  • http://rtdptech.com rtdp

    cool, that works like a charm.

  • http://whizcreed.com Aditya

    Thanks for sharing this.. It really helped..

  • Pingback: HTML ERB to HAML conversion | WhizCreed

  • http://gajendrajena.com Gajendra

    awesome!!!