javascript - Specify options for a filter in ruby HAML - Stack Overflow

Is there any way to add options (HTML attributes) to HAML filters?I wanted to do something like this ::

Is there any way to add options (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 would 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 drawback is that you can't indent your JS in HAML unless you're using the :javascript filter. It's ok for a few lines, but it can get messy quickly.

I'm well aware that in most cases if you end up with a plex script in a HAML template, it means you're doing something wrong and that's not the answer I'm looking for.

Is there any way to add options (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 would 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 drawback is that you can't indent your JS in HAML unless you're using the :javascript filter. It's ok for a few lines, but it can get messy quickly.

I'm well aware that in most cases if you end up with a plex script in a HAML template, it means you're doing something wrong and that's not the answer I'm looking for.

Share Improve this question asked Jan 30, 2014 at 7:44 JimJim 1,06211 silver badges24 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

There is no way to pass extra attributes to the :javascript filter like this. You could however use a :plain filter along with a normal script tag to allow indenting your javascript code:

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

produces:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745412600a4626602.html

相关推荐

  • javascript - Specify options for a filter in ruby HAML - Stack Overflow

    Is there any way to add options (HTML attributes) to HAML filters?I wanted to do something like this ::

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信