11

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 :)

ylluminate
  • 12,102
  • 17
  • 78
  • 152
Khmaies Guesmi
  • 111
  • 1
  • 1
  • 4

7 Answers7

29

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

Original answer

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

Dorian
  • 7,749
  • 4
  • 38
  • 57
Joel AZEMAR
  • 2,506
  • 25
  • 31
  • 1
    I had to convert all erb templates to slim in some project, and wow! Your tiny script is soo helpful!! :-) Thank you sir! – Szymon Przybył Jul 22 '13 at 09:58
  • awesome. Just one tip if you are using mac `find . -name '*haml' | xargs ruby -e 'ARGV.each { |i| puts "haml2slim #{i} #{i.sub(/haml$/,"slim")}"}' | sh` .. make it single line and use sh – Gaurav Shah Sep 20 '13 at 01:36
  • "You must pass through HAML ! " Why? – ahnbizcad Jul 27 '14 at 10:33
  • 2
    note for rails 4 and as of 2014 sept: you don't need `haml` gem itself, and you don't need `hpricot` gem when converting. It is enough to have `ruby_parser`, `html2haml`, and `haml2slim` for converting existing files. To have future views to be generated in slim, simply use `slim-rails` gem, and your views will be in slim whenever you run generators. Also, you can use gemfiles and run bundle, rather than `gem uninstall yourgem` – ahnbizcad Sep 10 '14 at 16:09
  • I did some modifications based on your original answer. Check it out: http://stackoverflow.com/a/26073207/784318. Thanks for the original script – Besi Sep 27 '14 at 10:04
  • I'm using rails 4. I tried this, and got all the .slim files instead of the erb ones as they were all deleted but now, im getting the missing template error... To convert from ERB to Slim is there anything else I need to know besides changing the views files for a RoR app? Thanks. – hello_its_me Oct 28 '14 at 17:35
  • The last version (0.8.0) of haml-rails provide rake haml:erb2haml for the first part and obviously you're under rails app. – Joel AZEMAR Feb 09 '15 at 15:44
6

Here you are, just paste the erb code and hit 'go':
http://html2slim.herokuapp.com/

ted
  • 187
  • 1
  • 7
  • good for short stuff. But if you want to do all your files in your entire project, a tool for mass conversion is needed not only to convert code, but to rename files without having to do it manually. – ahnbizcad Jul 27 '14 at 10:41
  • And to have future generated templates be in slim. – ahnbizcad Aug 20 '14 at 07:38
6

This is based on @Joel's brilliant answer. I had to modify it a bit since some gems seem to have moved and I made some other improvements:

  • It is all one script so just copy paste
  • Don't delete the gems at the end, since I will likely need this for the next project (e.g. when I create devise's views)
  • The gem install ... portion can then be emitted to make for faster processing.

Converting the files

Update: The conversion through haml is no longer required. This is the updated script:

#### gem install html2slim # this will install `erb2slim` command line tool.
find . -name '*erb' | \
xargs ruby -e 'ARGV.each { |i| puts "erb2slim #{i} #{i.sub(/erb$/,"slim")}"}' | \
bash

# Clean ERB templates
find . -name '*erb' -exec rm -f {} \;
git add app/views/*
git commit -m "Replace erb with slim"

The result

In my example (after running rails g devise:views)all the .erb files were replaced with .slim files and then deleted:

Screenshot

The alternative for single files

Sometimes I just wan to convert a snipped. Like it was mentioned before. In such cases I use

https://html2slim.herokuapp.com

html2slim-in-action

The old approach

So here we go:

# You must pass through HAML !
# Install HAML dependencies on your environment or your gemset
gem install haml html2haml hpricot ruby_parser haml2slim

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

#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 {} \;
Besi
  • 22,579
  • 24
  • 131
  • 223
5

I like the de facto answer but thought people would love to know about a new gem which does this much faster and with less hassle. It is at the moment however fairly buggy. :(

Check out html2slim. Say I want to change all my views from .erb to .slim, then I run (from scratch, and from your rails root directory) the following:

gem install html2slim
erb2slim app/views --delete

If you run erb2slim -h you can see that -d/--delete is an option to delete the erbs afterwards and --trace shows a full traceback on any errors. A note from the author that it is still experimental.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
1

Simply rename the file to end with .html.slim instead of .html.erb, and replace the content with something like the following:

strong.result= "Votes: #{voteable.votes_for - voteable.votes_against}"

= form_tag user_votes_path(current_user) do
  = radio_button_tag :thumb_direction, :up
  = radio_button_tag :thumb_direction, :down
  = hidden_field_tag :voteable, @voteable
  = submit_tag :vote
Digitalex
  • 1,494
  • 9
  • 11
0

This is online tool does exactly what you want http://erb2slim.herokuapp.com is convert erb snippets to slim.

si le
  • 104
  • 1
  • 2
0

As in previous answers I use two gems:

 gem install html2haml haml2slim

Then:

 find app/views -name \*.erb -print | sed 'p;s/.erb$/.haml/' | xargs -n2 sh -c 'html2haml "$0" "$1" && rm "$0"'

to replace *.erb with converted *.haml versions.

Now to convert *.haml to *.slim and remove *.haml files:

 haml2slim -d app/views

After all optionally:

 gem uninstall html2haml haml2slim
evilguc
  • 910
  • 8
  • 14