Conditional comments for Internet Explorer with Haml

Updated . Posted . Visible to the public.

Internet Explorer 5+ is aware of conditional comments Show archive.org snapshot that let you target HTML for selected versions of IE. For example the HTML below would ask users of IE 6 and IE 7 to install Firefox:

<!--[if lt IE 8]>
  <a href='http://www.mozilla.com/en-US/firefox/'>Get a real browser</a>
<![endif]-->

You might wonder how to express such a conditional comment in your favorite templating language, Haml Show archive.org snapshot . You might even have converted a template back to ERB just for this reason! But don't worry any longer, because Haml can do conditional comments Show archive.org snapshot :

/[if lt IE 8]
  %a{ :href => 'http://www.mozilla.com/en-US/firefox/' }= 'Firefox'

Sometimes you may need to insert semi-conditional HTML that is ignored by IE but interpreted by real browsers, like:

<%= stylesheet_link_tag 'screen', :media => 'screen' %>

Such content cannot be expressed in Haml -- you need to open up an :erb section in your Haml file to put it there.

Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2011-05-12 19:03)