Parris
Parris

Reputation: 18408

HAML Having Multiple Lines While Using content_for, for JavaScript

I am trying to output some inline js on a page. I don't really want to add it to any JS file since it is so aribtrary and one time use. That being said, I am using haml and also trying to use content_for in order to place the JS after jquery is loaded from the layout.

The problem is that haml does not like multiline text that is indented (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 have:

  = yield(:javascript)

Which actually works if I only have 1 line after the content_for statement.

Upvotes: 8

Views: 8902

Answers (2)

phil pirozhkov
phil pirozhkov

Reputation: 4900

This is called escaping, and in this case you escape, 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"); / }); / });

Upvotes: 0

iwasrobbed
iwasrobbed

Reputation: 46703

The correct syntax would be:

- 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");
      }); 
    });

Notice the dash versus the equal sign and the :javascript HAML filter. HAML works just fine with multi-line as long as it is 2-space indented.

And then in the other part of your view:

= content_for(:javascript)

Upvotes: 16

Related Questions