“[ !IE]” conditional comments in Haml

后端 未结 4 1730
忘掉有多难
忘掉有多难 2020-12-05 02:53

In a HAML doc, I have:

/[if IE]
  This is IE
/[if !IE]
  This is not IE

The first conditional evaluates properly in IE (and presumably in F

4条回答
  •  余生分开走
    2020-12-05 03:15

    When using IE conditional comments, there are two different types you need to be aware of. First, when the entire content is inside a HTML comment (between ), but IE will read it because of the condition:

    
    

    The other type is not a single comment, but some content that browsers will see, surrounded by two comments that will make IE ignore it:

    
      This is not a HTML comment, so browsers should see it, but IE will ignore it.
    
    

    (SO's code highlighting shows the difference - in the top one everything is grey as it's all comment, but in this one the text is darker as it's not a comment).

    The Haml support for IE conditional comments is only useful for creating the first kind, as it is part of the syntax for creating block comments. If you try using it for the second kind (as you have here) you get something like:

    
    

    which is effectively an unconditional comment.

    In order to use the [if !IE] type in Haml, you'll probably have to do it manually:

    %p Some other content
    
    %p
      Here's some content that shouldn't appear in IE.
    
    

    You could also make use of the Haml surround helper, like this:

    %p Some other content
    =surround '', '' do
      %p
        Here's some content that shouldn't appear in IE.
    

    (If you’re using Rails then you’ll need to use html_safe on the strings, i.e. surround ''.html_safe, ''.html_safe do).

    If you're using this a lot, it might be worth creating a helper method that wraps this call to surround.

提交回复
热议问题