为什么不在这个自动关闭的haml_tag上设置属性?

时间:2014-07-16 13:43:31

标签: haml

我正在使用辅助方法haml_tag捕获haml输出。但是,我想要捕获的特定元素是<meta/>标记,因此我试图弄清楚如何获得自我关闭标记并包含属性。

这很有效(差不多)

haml_tag 'meta', '', name: :description, content: 'this is the description'

输出:

<meta content='this is the description' name='description'></meta>

但我想输出:

<meta content='this is the description' name='description' />

这种自我关闭但不打印属性......

如果您传递了:/的标记,那么应该可以自动关闭标记,但在这种情况下,属性不会被打印出来:

haml_tag 'meta', nil, :/, name: :description, content: 'this is the description'

输出:

<meta />

我怎样才能充分利用这两个世界?

1 个答案:

答案 0 :(得分:1)

如果你想要一个空标签,只需省略第二个参数:

haml_tag 'meta', name: :description, content: 'this is the description'

产生

<meta content='this is the description' name='description' />

请注意,由于meta是默认的空元素之一,因此您不需要:/参数,但如果您想要显式,则可以添加它:

haml_tag 'meta', :/, name: :description, content: 'this is the description'

产生与上面相同的输出。

Haml 4.0.5中存在一个错误,因为haml_tag不尊重format选项。在HTML格式中,输出应该是:

<meta content='this is the description' name='description'>

This is fixed in 4.1.0.beta.1

相关问题