Converting all .html.erb views to .haml
After trying haml and sass on a few occasions, I am sold. Recently, I needed to convert all my /app/views/**/*.html.erb views into /app/views/**/*.haml and I ended up writing a rake task as follows:
require 'haml' require 'haml/exec' namespace :haml do task :convert do Dir.glob("app/views/**/*.html.erb").each do |html| puts html + "..." Haml::Exec::HTML2Haml.new([html, html.gsub(".html.erb", ".haml")]).process_result File.delete(html) end end end
This did the trick. There was only one minor thing I had to fix with the new *.haml files. The blocks with an “<%= end %>” were converted into “- end”. Instead the correct format was to not use “- end” and properly indent the code inside block.
So, for example, a .html.erb code as follows:
<% form_for @booking do |f| %> <%= f.error_messages %> <%= f.label :booking_reference %> <%= f.text_field :booking_reference %> <%= f.label :remarks %> <%= f.text_area :remarks %> <p> <%= f.submit "Submit" %> </p> <% end %>
was converted to:
- form_for @booking do |f| = f.error_messages = f.label :booking_reference = f.text_field :booking_reference = f.label :remarks = f.text_area :remarks %p = f.submit "Submit" - end
Instead, it should have been:
- form_for @booking do |f| = f.error_messages = f.label :booking_reference = f.text_field :booking_reference = f.label :remarks = f.text_area :remarks %p = f.submit "Submit"
Once I fixed this manually (and it could be a lot if you have lots of files), it all worked as expected.
No related posts.
Related posts brought to you by Yet Another Related Posts Plugin.





