在ruby HAML中指定过滤器的选项 [英] Specify options for a filter in ruby HAML

查看:166
本文介绍了在ruby HAML中指定过滤器的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想这样做:

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

结果是:

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

我能得到的最接近的是:

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





$ b

缺点是你不能在HAML中缩进你的JS,重新使用:JavaScript过滤器。我很清楚,在大多数情况下,如果您最终在HAML模板中使用复杂的脚本,那么您可能会遇到一些问题。 ,这意味着你做错了,这不是我正在寻找的答案。

解决方案

没有办法将额外的属性传递给:javascript 过滤器。但是,您可以使用:plain 过滤器以及普通的脚本标记来缩进您的JavaScript代码:

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

$ / code>

产生:

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


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.

解决方案

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>

这篇关于在ruby HAML中指定过滤器的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆