Convert ERB template to SLIM

Khmaies Guesmi picture Khmaies Guesmi · Apr 27, 2012 · Viewed 11.8k times · Source

Many of my views are SLIM templates and I wish to add a vote_form partial to my app. How would I convert this partial view from ERB to SLIM?

<strong class="result">Votes: <%= voteable.votes_for - voteable.votes_against %></strong>
<%= form_tag user_votes_path(current_user) do |f| %>
  <%= radio_button_tag :thumb_direction, :up %>
  <%= radio_button_tag :thumb_direction, :down %>
  <%= hidden_field_tag :voteable, @voteable %>
  <%= submit_tag :vote %>
<% end %>

Thanks :)

Answer

Joel AZEMAR picture Joel AZEMAR · Jan 24, 2013

How convert your .erb to .slim :

Update! 18-08-2015

You can simply use html2slim gem

gem install html2slim

This package include a tool called erb2slim which can convert erb file to slim recursively. Option -d for delete the erb file after the convert finished.

erb2slim -d <dir of your views>

View on devise wiki

End of Update!

You must pass through HAML !

Install HAML dependencies on your environment or your gemset

gem install html2haml # This was moved out of haml gem
gem install ruby_parser

Switch to HAML templating

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

Install SLIM tool dependency

gem install haml2slim # https://github.com/fredwu/haml2slim

Switch to SLIM templating

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

Clean ERB and HAML templates

find . -name '*erb' -exec rm -f {} \;
find . -name '*haml' -exec rm -f {} \;

Remove dependencies

gem uninstall html2haml
gem uninstall ruby_parser
gem uninstall haml2slim

That all, have fun