Rails 3& HTML 5 Microdata [英] Rails 3 & HTML 5 Microdata

查看:121
本文介绍了Rails 3& HTML 5 Microdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails中,是否可以使用任何ActionView Helper创建HTML 5无值属性?我正在尝试为Google的面包屑的。这是我想要生成的输出:

 < div itemscope itemtype =http://data-vocabulary.org /面包屑>< / DIV> 

但是您可以看到 itemscope 属性值没有值。理想情况下,我想在Rails中这样做......

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

...但我似乎无法生成没有值的属性。

解决方案

不,你不能。 Rails的标签助手定义一个 ActionView :: Helpers :: TagHelper :: BOOLEAN_ATTRIBUTES 数组,该数组中的任何属性都将被输出为 key = key ,所以如果你有标签(:输入,:type =>:复选框,:checked => true),输出应为< input type ='checkbox'checked ='checked'/>
$ b p我认为这个解析正确,因为XML解析器只是要检查属性的 presence ,所以任何值都应该有效。



为此,你可以使用:

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

这将输出 itemscope =itemscope in标记,但它在解析时应该产生相同的预期效果。



或者,y你可以将itemscope添加到BOOLEAN_ATTRIBUTES,然后指定:itemscope =>真正的


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.

解决方案

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.

这篇关于Rails 3&amp; HTML 5 Microdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆