Ruby-on-rails 如何使用haml和rails做数据属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24353788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:44:25  来源:igfitidea点击:

How to do data- attributes with haml and rails?

ruby-on-railsruby-on-rails-3hamlcustom-data-attributehtml5-data

提问by Michael Durrant

I can have

我可以有

%a{href: '#', data_toggle_description_length: 'toggle_me_ajax'}

which it gives me underscores not dashes, i.e.

它给了我下划线而不是破折号,即

<a href="#" data_toggle_description_length="toggle_me_ajax"></a>

However I want to have HTML5 data-attributes, i.e.

但是我想拥有HTML5 data-属性,即

<a href="#" data-toggle-description-length="toggle_me_ajax"></a>

but when I try replacing underscores with dashes, i.e.

但是当我尝试用破折号替换下划线时,即

%a{href: '#', data-toggle-description-length: 'toggle_me_ajax'}

I get syntax errors:

我收到语法错误:

/home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_links.html.haml:13: syntax error, unexpected tLABEL
...data-toggle-description-length: 'toggle_me_ajax')}>\n    tog...
...                               ^
/home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_links.html.haml:13: syntax error, unexpected ')', expecting '}'
...ption-length: 'toggle_me_ajax')}>\n    toggleMeAjax\n  </a>\...
...                               ^
/home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_links.html.haml:13: unknown regexp options - pa
/home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_links.html.haml:13: syntax error, unexpected $undefined
... toggleMeAjax\n  </a>\n</span>\n", -1, false);::Haml::Util.h...
...                               ^
/home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_links.html.haml:13: unterminated string meets end of file
/home/durrantm/Dropnot/webs/rails_apps/linker/app/views/links/_links.html.haml:13: syntax error, unexpected $end, expecting '}'

回答by Mandeep

Try this:

尝试这个:

%a{"data-toggle-description-length" => "toggle_me_ajax", href: "#"}

OR

或者

%a{href: "#", :data => {:toggle_description_length => "toggle_me_ajax"}}

For more details refer here

有关更多详细信息,请参阅此处

You can also use html2haml converteravailable online

您还可以使用在线提供的html2haml 转换器

EDIT:

编辑:

As mentioned in comments there are a couple more syntaxes which would work

正如评论中提到的,还有更多的语法可以使用

 %a{href: "#", { "data-toggle-description-length": "toggle_me_ajax" }}

OR

或者

%a{href: "#", { :"data-toggle-description-length" => "toggle_me_ajax" }}

I would still prefer first two though as I think latter ones look ugly and kinda messy.

我仍然更喜欢前两个,因为我认为后一个看起来丑陋而且有点凌乱。

回答by ocodo

There's really not much need to use { ... }style in haml. HTML style attributes are a more flexible and natural way for html generation.

{ ... }在haml中真的没有太多需要使用样式。HTML 样式属性是一种更灵活、更自然的 html 生成方式。

%a(href="#" data-toggle="target") my link

No commas, no hash rockets etc. are required. You can also very easily interpolate or directly assign variables without switching styles.

不需要逗号,不需要哈希火箭等。您还可以非常轻松地插入或直接分配变量而无需切换样式。

e.g.

例如

%a(href=link data-toggle="#{id}-toggle")

Where linkand idare variables from the currently bound scope.

wherelinkid是来自当前绑定范围的变量。

Notably you can also seamlessly include attributes from xmlns, svg generation uses a lot of namespace prefixes for example:

值得注意的是,您还可以无缝地包含来自 xmlns 的属性,svg 生成使用了很多命名空间前缀,例如:

%link(xlink:type="simple" xlink:href=link)

There's no compelling reason to use another style.

没有令人信服的理由使用另一种风格。