HAML has multiple lines when using content_for, for JavaScript

I am trying to output some inline js on a page. I really do not want to add it to any JS file, because it is so arbitrary and used once. In doing so, I use haml and also try to use content_for to post JS after loading jquery from the layout.

The problem is that haml doesn't like multiline indented text (I think)

I am trying to do the following:

=content_for :javascript do $(function(){ $("#sell_tickets_here").live("vclick",function(){ if($(this).is("checked")) $("#tickets_here").display("inline"); else $("#tickets_here").display("none"); }); }); 

In my layout, I:

  = yield(:javascript) 

Which really works if I have only one line after the content_for statement.

+8
javascript ruby-on-rails haml
source share
2 answers

The correct syntax is:

 - content_for :javascript do :javascript $(function(){ $("#sell_tickets_here").live("vclick",function(){ if($(this).is("checked")) $("#tickets_here").display("inline"); else $("#tickets_here").display("none"); }); }); 

Note the dash against the equal sign and :javascript HAML filter . HAML works just fine with multi-line if it is indented with 2 spaces.

And then in another part of your view:

 = content_for(:javascript) 
+15
source share

This is called escaping , in which case you run, ergh, indentation:

=content_for :javascript do / $(function(){ / $("#sell_tickets_here").live("vclick",function(){ / if($(this).is("checked")) / $("#tickets_here").display("inline"); / else / $("#tickets_here").display("none"); / }); / });

0
source share

All Articles