Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 & HTML 5 Microdata

In Rails, is it possible to create a HTML 5 valueless attribute using any of the ActionView Helpers? I'm trying to create the HTML 5 itemprop microdata for google's BreadCrumbs. Here's the output I'd like to generate:

<div itemscope itemtype="http://data-vocabulary.org/Breadcrumb"></div>

But you can see that the itemscope attribute value has no value. Ideally, I'd like to do something like this in Rails...

content_tag(:div, "somecontent", :itemscope => nil, :item_type => "http://data-vocabulary.org/Breadcrumb") 

... but I can't seem to get generate an attribute without a value.

like image 659
dhulihan Avatar asked Feb 01 '11 20:02

dhulihan


1 Answers

No, you can't. Rails' tag helper defines a ActionView::Helpers::TagHelper::BOOLEAN_ATTRIBUTES array, and any attributes in that array will be outputted as key=key, so if you had, say, tag(:input, :type => :checkbox, :checked => true), the output should be <input type='checkbox' checked='checked' />.

I presume that this parses properly, since an XML parser is simply going to check for the presence of the attribute, so any value should work.

To that end, you could use:

content_tag(:div, "somecontent", :itemscope => "itemscope", :item_type ...

This will output itemscope="itemscope" in the tag, but it should result in the same desired effect when parsed.

Alternately, you could add itemscope to the BOOLEAN_ATTRIBUTES, then specify :itemscope => true.

like image 62
Chris Heald Avatar answered Oct 07 '22 14:10

Chris Heald