johnmcaliley
johnmcaliley

Reputation: 11055

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

Upvotes: 9

Views: 3599

Answers (3)

Docunext
Docunext

Reputation: 803

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.

Upvotes: 1

Nello
Nello

Reputation: 31

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.

Upvotes: 2

johnmcaliley
johnmcaliley

Reputation: 11055

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

Upvotes: 10

Related Questions