Ruby HAML的空属性 [英] Empty attribute with Ruby HAML

查看:81
本文介绍了Ruby HAML的空属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HAML在Ruby项目上实现Schema微格式,但无法弄清楚如何在标记上设置空属性.我尝试了nil和false,但它们根本没有显示.

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 not shown.

示例:<div itemscope>

我正在尝试设置一个空的itemscope属性.

I'm tring to set an empty itemscope attribute.

我的代码:

.agency.premium{:itemscope => true, :itemtype => 'schema.org/ProfessionalService';} 

:itemscope => true似乎是HAML文档中推荐的方法.我得到的结果与:itemscope => ''相同,:itemscope => ''是具有空值(即<div itemscope="">)的XHTML有效属性.

:itemscope => true seems to be the recommended approach from HAML's documentation. I get the same result as I would get with :itemscope => '', a XHTML-valid attribute with an empty value (i.e. <div itemscope="">).

可能还不错,但是我希望它像Schema文档中记录的那样为空.

Probably fine, but I'd rather have it empty as is documented in the Schema doc.

推荐答案

使用类似

%div{:itemscope => true}

是在Haml文件中进行指定的正确方法.

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

呈现方式取决于您如何设置Haml的 选项. Haml 3.1中的默认值为xhtml,使用它将默认显示为itemprop='itemprop',这是有效的xhtml.要使用最小化的属性(例如<div itemscope>)进行渲染,您需要将格式设置为html4html5. (在Rails 3中,默认值为html5,在Haml 4.0中,默认值为html5.)

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 <div itemscope>) 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).

如何设置Haml选项取决于您的使用方式,请参见.

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

例如,直接在Ruby中使用Haml,这是

For example, using Haml directly in Ruby, this:

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

产生具有完整属性的默认xhtml:

produces the default xhtml with full attributes:

<div itemscope='itemscope'></div>

但是这个:

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

使用最少的属性产生所需的结果:

produces the desired result with minimized attributes:

<div itemscope></div>

这篇关于Ruby HAML的空属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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