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 :)
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>
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
Here you are, just paste the erb code and hit 'go':
http://html2slim.herokuapp.com/
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:
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
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 {} \;
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.
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
This is online tool does exactly what you want http://erb2slim.herokuapp.com is convert erb snippets to slim.
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
来源:https://stackoverflow.com/questions/10347572/convert-erb-template-to-slim