Empty attribute with Ruby HAML

后端 未结 3 632
余生分开走
余生分开走 2020-12-14 06:01

I\'m implementing Schema microformats on a Ruby project using HAML and can\'t figure out how to set an empty attribute on a tag. I tried nil and false, but they simply do no

3条回答
  •  失恋的感觉
    2020-12-14 06:25

    Using something like

    %div{:itemscope => true}
    

    is the correct way to specify this in your Haml file.

    How this is rendered depends on how you set Haml's format option. The default in Haml 3.1 is xhtml, and with that it will render as itemprop='itemprop', which is valid xhtml. To render with minimized attributes (like

    ) you need to set the format to html4 or html5. (In Rails 3 the default is html5, and in Haml 4.0 the default is html5).

    How to set the Haml options depends on how you are using it, see the options section in the docs.

    For example, using Haml directly in Ruby, this:

    engine = Haml::Engine.new '%div{:itemscope => true}'
    puts engine.render
    

    produces the default xhtml with full attributes:

    But this:

    engine = Haml::Engine.new '%div{:itemscope => true}', :format => :html5
    puts engine.render
    

    produces the desired result with minimized attributes:

提交回复
热议问题