39

I have a rails create action which send back some jquery in a file:

create.js.erb

var appearance = $("<%= escape_javascript(render(:partial => @appearance)) %>").effect("highlight", {color: '#E6ff00'}, 2000);
$("#sortable").append(appearance);
$("#new_appearance")[0].reset();

I have started using HAML and want to know how I should be converting this. Can I use js.haml? If so what should the markup look like?

Cameron
  • 4,181
  • 9
  • 36
  • 40

4 Answers4

77

Actually, returning JS in HAML is pretty easy, just use the :plain filter and enclose anything you want evaluated in #{}.

:plain
  var appearance = $("#{escape_javascript(render(:partial => @appearance)}").effect("highlight", {color: '#E6ff00'}, 2000);
  $("#sortable").append(appearance);
  $("#new_appearance")[0].reset();

Do not use the :javascript filter as it will enclose everything in a tag which is unnecessary.

If you need to use some logic, just put the :plain inside the nesting.

- if params[:printing]
  :plain
    $('#print-view').html("#{escape_javascript(render 'print_preview')}");

Sorry ERB.

Karl
  • 1,585
  • 2
  • 13
  • 22
4

Haml's really meant to generate HTML. You could, in theory, use create.js.haml, but ERB makes much more sense when you're writing Javascript.

Natalie Weizenbaum
  • 5,884
  • 28
  • 22
  • 10
    Care to explain why using ERB makes "much more sense" when you're writing Javascript? I find HAML as good, cleaner and works with Coffeescript as well. ty – Florin Jun 05 '11 at 18:54
  • 2
    @Florin Because Haml is designed specifically for the syntax of HTML and XML. When you generate anything else with it, you need to do too much escaping or otherwise working around the fact that the output isn't HTML or XML. Haml's cleaner all right, but only for HTML/XML. – Marnen Laibow-Koser Apr 20 '12 at 05:45
  • 1
    no escaping required if you can use filters such as :javascript or :plain, see the accepted answer above. – Florin Apr 23 '12 at 16:55
  • @Florin But that really only works when embedded in HTML or XML. If you need to generate a document that isn't at least encapsulated in HTML or XML, then there's no point in using Haml. – Marnen Laibow-Koser May 29 '12 at 16:23
2

Well looks like you dont need the != in front of each line. The following works for me just fine. Im using Rails 3 and latest version of HAML

$('#comments_table_header').after('#{escape_javascript(render :partial => 'row', :locals => { :comment => @comment })}');
Mohith Thimmaiah
  • 857
  • 1
  • 9
  • 21
0

In a js.haml it would look something like

!= "var appearance = $('<%= escape_javascript(render(:partial => @appearance)) %>').effect('highlight', {color: '#E6ff00'}, 2000);"
!= "$('#sortable').append(appearance);"
!= "$('#new_appearance')[0].reset();"

Note the HAML != syntax which prevents the generated text from being HTML escaped. I do agree that ERB is a better fit here, but for consistency stake you might still want to go the HAML way.

loafoe
  • 363
  • 2
  • 11