布尔属性,在 html 5 草案规范中定义:
http://dev.w3.org/html5/spec/Overview.html#boolean-attributes
元素上存在布尔属性表示真值,不存在该属性表示假值。
如果存在该属性,则其值必须是空字符串或与属性的规范名称匹配的不区分大小写的 ASCII 值,并且没有前导或尾随空格。
我的页面使用的是 html5 DTD。我正在尝试在我自己的助手中使用 content_tag 视图助手,但是在将布尔属性传递给它时遇到了问题。
具体来说,这是我的帮手:
def itemscope(type, options = {}, &block)
content_tag(
:div, {
:itemscope => true,
:itemtype => data_definition_url(type)
}.merge(options),
true,
&block
)
end
def data_definition_url(type)
"http://data-vocabulary.org/#{type}"
end
在我看来,假设我这样称呼它(我正在使用haml):
= itemscope("Organization") do
%h1 Here's some content
这就是我希望它呈现的内容:
<div itemscope itemtype='http://data-vocabulary.org/Organization'>
<h1>Here's some content</h1>
</div>
但这是我实际得到的:
<div itemscope='true' itemtype='http://data-vocabulary.org/Organization'>
<h1>Here's some content</h1>
</div>
根据 w3 规范,这是无效的标记。布尔属性的合法值是属性本身的名称,或者根本没有值。
这很烦人,因为我可以更改:itemscope => true
to:checked => true
并且它将正确地呈现属性,就像checked='checked'
div 元素的属性列表中一样。
我更希望它呈现just itemscope
...的最小化版本,但我不确定如何使用 content_tag 选项来做到这一点。我可以很容易地发送 :itemscope => 'itemscope' 但是很难说这是否会被谷歌正确解释,因为他们的所有示例和规范都显示了最小化版本。请参阅此处: http ://www.google.com/support/webmasters/bin/answer.py?answer= 146861 了解这些属性是什么以及我使用它们的原因(微数据格式)
任何人都知道我如何有效地让任何属性在 content_tag 中发送一个真或假(ruby boolean)值以在没有任何值的情况下呈现,而不是尝试对布尔值进行字符串化?谢谢 :)