HAML: Create data-xxx-yyy attribute

11,291

You'll have to use

:data => {'user-name' => 'John', 'user-age' => 24}

The data attribute is special-cased by HAML and it only accounts for shallow values.

Share:
11,291

Related videos on Youtube

htaidirt
Author by

htaidirt

Curious 🧐

Updated on October 08, 2022

Comments

  • htaidirt
    htaidirt over 1 year

    I know how to create a link with a data-* attribute:

    %a{ :href => "#", :data => { :name ="John", :age => 24 } } Hi John
    

    generates:

    <a href="#" data-name="John" data-age="24">Hi John</a>
    

    But how about a 2 deep data name, like data-user-name and data-user-age, a way to group data attributes. As you may guess, I tried:

    :data => { :user => { :name => "John", :age => 24 } }
    

    But it doesn't work, giving me strange HTML output:

    <a href="#" data-user="nameJohnage24">Hi John</a>
    

    Any idea how to do that? Thanks in advance.

  • htaidirt
    htaidirt almost 12 years
    So, it's not possible to group user- the way we grouped data-?
  • Casey Foster
    Casey Foster almost 12 years
    No, because the data attribute is special-cased by HAML and it only accounts for shallow values. You could open an issue at github.com/haml/haml to spark some discussion on it. I didn't look but I wouldn't be surprised if it had been mentioned before.