How to do data- attributes with Haml and Rails?
Asked Answered
K

3

39

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 HTML data- attributes, i.e.

<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 '}'
Kore answered 22/6, 2014 at 17:26 Comment(0)
R
73

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 converter available online

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.

Roderickroderigo answered 22/6, 2014 at 17:29 Comment(6)
+1 Nice catch! Yeah looks like rails will convert the underscore to dash for the second form. I like that second form so glad I can use it.Kore
Any reason not to use the newer hash syntax there? (seems ok for cases I am trying).Kore
@MichaelDurrant both will generate same html so i don't really think it matters. I just thought i should write both ways so you can choose yourselfRoderickroderigo
Another option would be to use the HTML style attributes: %a(href= '#' data-toggle-description-length: 'toggle_me_ajax'). Also converting underscores to hyphens may well be the norm in the future, not just for nested hashes as it is currently: github.com/haml/haml/issues/782.Centuplicate
Also: { "data-toggle-description-length": "toggle_me_ajax" } or even { :"data-toggle-long-data-attribute" => "tickle_me_elmo" } if you like rockets.Uno
Using Ruby 1.9 hashes, you can do it this way: .your_div{id: "the_id", class: "the_class", data: { some_data: "your data goes here"}} No old style hash rockets, no strings, and the data attribute is properly formatted in the HTML.Jiggered
S
11

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

%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 link and id are variables from the currently bound scope.

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

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

There's no compelling reason to use another style.

Sweven answered 6/10, 2015 at 13:46 Comment(4)
Because there are no commas separating attributes, though, more complicated expressions aren’t allowed. For those you’ll have to use the {} syntax from haml.info/docs/yardoc/file.REFERENCE.html#htmlstyle_attributes_Seymour
Your templates should not have complex expressions, it adds unnecessary problems during maintenance of the app.Sweven
Not sure why this isn't the accepted answer. This is way better than using the general-purpose syntax of Ruby for the specific task of setting HTML attributes. For all that's wrong with HTML, attribute syntax isn't really an issue.Korfonta
@AndrewKoster mostly becuase the answer came in more than a year after the original. Note that the accepted answer now has 67 votes so not sure if I should change it anywayKore
D
0

Something like this should work really well:

%a{ "data-user-id" => "#{@user.id}" }

Diapason answered 7/1, 2021 at 20:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.