Tatva-Artha

meaning of "it"

Converting all .html.erb views to .haml

without comments

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.

http://www.tatvartha.com/wp-content/plugins/sociofluid/images/digg_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/reddit_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/stumbleupon_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/delicious_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/google_16.png http://www.tatvartha.com/wp-content/plugins/sociofluid/images/twitter_16.png

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Written by Sharad

July 26th, 2009 at 7:47 pm

Posted in All

Leave a Reply