Specify filter parameters in ruby ​​HAML

Is there a way to add parameters (HTML attributes) to HAML filters?

I wanted to do something like this:

:javascript{:'data-turbolinks-eval' => 'false', :foo => 'bar'} if(someCondition){ doSomething(); } 

And the result will be:

 <script 'data-turbolinks-eval'='false' 'foo'='bar'> if(someCondition){ doSomething(); } </script> 

The closest I could get is:

 %script{:'data-turbolinks-eval' => 'false', :foo => 'bar'} if(someCondition){ doSomething(); } 

The downside is that you cannot backtrack from JS in HAML unless you use the javascript filter. This is fine for multiple lines, but it can get confused quickly.

I am well aware that in most cases, if you finish a complex script in a HAML template, it means that you are doing something wrong and that is not the answer I'm looking for.

+7
javascript ruby filter indentation haml
source share
1 answer

It is not possible to pass additional attributes to a filter :javascript like this. However, you can use the :plain filter along with a regular script tag to allow padding from your javascript code:

 %script{:'data-turbolinks-eval' => 'false', :foo => 'bar'} :plain if(someCondition()) { doSomething(); } 

gives:

 <script data-turbolinks-eval='false' foo='bar'> if(someCondition()) { doSomething(); } </script> 
+7
source share

All Articles