Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

br tag not closing in Haml on Rails 3

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??

like image 343
johnmcaliley Avatar asked Nov 02 '10 15:11

johnmcaliley


3 Answers

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

like image 86
johnmcaliley Avatar answered Nov 05 '22 05:11

johnmcaliley


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.

like image 28
Nello Avatar answered Nov 05 '22 06:11

Nello


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.

like image 34
Docunext Avatar answered Nov 05 '22 04:11

Docunext