br tag not closing in Haml on Rails 3

瘦欲@ 提交于 2019-12-08 15:56:04

问题


I am having a problem getting Haml to close br tags. I have tried the following with no luck:

%br  
%br/

I expect this to result in <br />, but it always outputs as <br>, even with the slash character on the end. I have also tried adding the following options to application.rb (and I tried environment.rb)

Haml::Template.options[:autoclose] = ['meta', 'img', 'link', 'br', 'hr', 'input', 'area', 'param', 'col', 'base']

Am I missing something? I though Haml was supposed to autoclose these tags by default??


回答1:


Ok, I found out the problem. Haml outputs HTML5 by default when using Rails 3. I didn't realize that <br> was valid syntax in HTML5. I was trying to get this to pass the W3C semantic extractor, so I need <br /> instead. In order to get this to work, you will need to update the Haml options for autoclose and set it to xhtml. Drop this code into your application.rb inside the class.

Haml::Template.options[:format] = :xhtml

More info here:

http://github.com/nex3/haml/issuesearch?state=closed&q=close#issue/155




回答2:


But, if I want xhtml5 (i.e. html5 with autoclose) there's no way to do it! I, like many other users, have tried overriding the :autoclose list and it just doesn't work.




回答3:


According to the haml docs:

Haml::Template.options[:format] = :xhtml

should be placed in config/environment.rb.

Placing it in environment.rb works for me.



来源:https://stackoverflow.com/questions/4079232/br-tag-not-closing-in-haml-on-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!