Looks like I'm pretty late for the party here, but maybe someone else will run into this and have to solve the same problem (as I did this evening).
In my case, I have a more complicated setup for opening an HTML tag and several different layouts, so I did not want a repetition. My opening HTML tag has conditions for different versions of IE and initially looked something like this:
- # /app/views/layouts/shared/_head.html.haml !!! 5 %html{ 'xml:lang' => 'en', lang: 'en', class: 'no-js'} %head - # and so on...
I had the same problem with </html> terminating prematurely, so I broke the HTML tag from the _head particle (leaving the main tag there) and created the following helper to solve the problem:
# /app/helpers/application_helper.rb module ApplicationHelper def render_html_tag(&block) markup = capture_haml &block haml = Haml::Engine.new <<-HAML !!! 5 <!--[if lt IE 7 ]> <html lang="en" class="no-js ie ie6"> <![endif]--> <!--[if IE 7 ]> <html lang="en" class="no-js ie ie7"> <![endif]--> <!--[if IE 8 ]> <html lang="en" class="no-js ie ie8"> <![endif]--> <!--[if IE 9 ]> <html lang="en" class="no-js ie ie9"> <![endif]--> <!--[if (gte IE 9)|!(IE)]><!--> %html{ 'xml:lang' => 'en', lang: 'en', class: 'no-js'} <!--<![endif]--> = markup HAML obj = Object.new haml.def_method(obj, :render, :markup) obj.render(markup: markup) end end
This is a bit dirty, and maybe you can clean it up a bit, but the main idea is to use the haml engine #def_method , which allows the layout to look something like this:
-
Anthony navarre
source share