2

I am trying to use an IE conditional comment based on the advice found this topic. "[ !IE]" conditional comments in Haml However it's not working, and I don't know why.

I am trying to edit a Ruby on Rails application and I am new to HAML and Rails. Any help would be appreciated.

Here is the HAML code I am trying to use.

%section#audio-controls
  =surround '<!--[if !IE]> -->'.html_safe, '<!-- <![endif]-->'.html_safe do  
    = audio_tag( '/audios/elder.wav', :controls => 'controls', :id => 'elder_audio' )
  =surround '<!--[if IE]> -->'.html_safe, '<!-- <![endif]-->'.html_safe do  
    = audio_tag( '/audios/elder.mp3', :controls => 'controls', :id => 'elder_audio' ) 

I have also tried this code, thinking it may be an issue with the audio_tag

%section#audio-controls
  =surround '<!--[if !IE]> -->'.html_safe, '<!-- <![endif]-->'.html_safe do  
    %p
      This is not IE
  =surround '<!--[if IE]> -->'.html_safe, '<!-- <![endif]-->'.html_safe do  
    %p
      This is IE
Community
  • 1
  • 1
rsnyder
  • 383
  • 1
  • 6
  • 19
  • @benedikt-deicke When using your code everything is commented out so it is effectively and unconditional comment. – rsnyder Jan 07 '13 at 23:07
  • Conditional comments are only supported in IE and therefore are treated as normal comments in all other browsers. You're right that the `if !IE` statement doesn't really make sense this way. – Benedikt Deicke Jan 07 '13 at 23:15

3 Answers3

3

HAML supports conditional comments using the /[] syntax.

Change your example to something like this and it should work as expected:

%section#audio-controls
  /[if !IE]
    %p This is not IE, but this won't be rendered as all other browsers don't know about conditional comments
  /[if IE]
    %p This is IE
Benedikt Deicke
  • 610
  • 3
  • 8
  • So is it even possible to get that first %p not to show up when viewed in IE, but still show up in other browsers? – rsnyder Jan 08 '13 at 00:14
  • Not with conditional comments alone. What you can do is add some JavaScript to the `/[if IE]` block, that hides the non-ie %p. – Benedikt Deicke Jan 08 '13 at 08:33
  • 2
    @rsnyder use the `/[if IE]` syntax for the `if IE` comments, and the `=surround ''.html_safe, ''.html_safe do` for the `!IE` comments. The point is that the `!IE` comments aren’t a single comment, you need two separate comments around the content. The `if IE` comments are a single comment, so the normal Haml comment syntax works for them. – matt Jan 09 '13 at 00:58
0

Use html2haml command line utility to convert HTML to HAML.

For example:

alhafoudh@Aluminium:~$ html2haml 
<!--[if IE 6]>
Special instructions for IE 6 here
<![endif]-->

^D
/[if IE 6]
  Special instructions for IE 6 here
Ahmed Al Hafoudh
  • 8,281
  • 1
  • 18
  • 34
0

for few conditions for me works this:

!!!
:plain
  <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]--><!--[if IE 7]><html class="no-js lt-ie9 lt-ie8" lang="en"><![endif]--><!--[if IE 8]><html class="no-js lt-ie9" lang="en"> <![endif]--> <!--[if IE 9]><html class="no-js lt-ie10" lang="en"> <![endif]--> <!--[if gt IE 9]><!-->
%html.no-js{:lang => 'en'}
  / <![endif]

  %head
    %title Yinlang
Anja Ishmukhametova
  • 1,535
  • 16
  • 14