Skip to content

Instantly share code, notes, and snippets.

@rstacruz
Created October 19, 2010 23:28
Show Gist options
  • Save rstacruz/635398 to your computer and use it in GitHub Desktop.
Save rstacruz/635398 to your computer and use it in GitHub Desktop.
cc_html.rb
# Implements the world-famous Paul Irish IE conditional comments HTML tag--in HAML.
# http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
class Main
helpers do
def cc_html(options={}, &blk)
attrs = options.map { |(k, v)| " #{h k}='#{h v}'" }.join('')
[ "<!--[if lt IE 7 ]> <html#{attrs} class='ie6'> <![endif]-->",
"<!--[if IE 7 ]> <html#{attrs} class='ie7'> <![endif]-->",
"<!--[if IE 8 ]> <html#{attrs} class='ie8'> <![endif]-->",
"<!--[if IE 9 ]> <html#{attrs} class='ie9'> <![endif]-->",
"<!--[if (gt IE 9)|!(IE)]><!--> <html#{attrs}> <!--<![endif]-->",
capture_haml(&blk),
"</html>"
].join('')
end
def h(str) Rack::Utils.escape_html(str); end
end
end
!!! 5
!= cc_html(:lang => 'en') do
%head
%meta(charset='UTF-8')
%title Hello!
%body
...
<!DOCTYPE html>
<!--[if lt IE 7 ]> <html lang="en" class="ie6"> <![endif]-->
<!--[if IE 7 ]> <html lang="en" class="ie7"> <![endif]-->
<!--[if IE 8 ]> <html lang="en" class="ie8"> <![endif]-->
<!--[if IE 9 ]> <html lang="en" class="ie9"> <![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--> <html lang="en" class=""> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>Hello!</title>
</head>
<body>
...
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment