Specify options for a filter in ruby HAML

﹥>﹥吖頭↗ 提交于 2020-01-02 03:01:10

问题


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 complex script in a HAML template, it means you're doing something wrong and that's not the answer I'm looking for.


回答1:


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>


来源:https://stackoverflow.com/questions/21450524/specify-options-for-a-filter-in-ruby-haml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!