Skip to content

Instantly share code, notes, and snippets.

@frank-who
Created February 26, 2012 00:49
Show Gist options
  • Save frank-who/1911952 to your computer and use it in GitHub Desktop.
Save frank-who/1911952 to your computer and use it in GitHub Desktop.
Adding IE version specific class names to the <html> tag without using conditional comments.
# initializers/app_configuration.rb
# Browser object used for UserAgent comparison
Browser = Struct.new(:browser, :version)
# Render indented HTML in development mode
if Rails.env.development?
Slim::Engine.set_default_options pretty: true
end
doctype 5
= html_tag do
head
meta charset="utf-8"
meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"
title Cool App
meta name="viewport" content="width=device-width"
= stylesheet_link_tag "application", media: "all"
= javascript_include_tag "modernizr"
= csrf_meta_tags
body
= javascript_include_tag "application"
module ApplicationHelper
def html_tag(options={}, &block)
css = ["no-js"]
browser = UserAgent.parse(request.user_agent)
ie7 = Browser.new("Internet Explorer", "7.0")
ie8 = Browser.new("Internet Explorer", "8.0")
if browser < ie7
css << ["oldie", "lt-ie7"]
elsif browser == ie7
css << ["oldie", "ie7"]
elsif browser == ie8
css << ["oldie", "ie8"]
end
output = ActiveSupport::SafeBuffer.new
output.safe_concat(tag(:html, {class: css, lang: I18n.default_locale}, true))
output << capture(&block)
output.safe_concat("</html>")
end
end
gem "slim"
gem "useragent"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment