How would I format HAML to output a style similar to the conditional HTML tags used for cross-browser targeting?
<!doctype html>
<!--[if lt IE 8]> <html class="no-js ie7 oldie" lang="en"> <![endif]-->
<!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]-->
<!--[if IE 9]> <html class="no-js ie9 oldie" lang="en"> <![endif]-->
<!--[if gt IE 9]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
If I add a conditional, it wraps entire page in the conditional as well. I thought about using HAML's :plain
filter at the top and manually adding </html>
at the bottom, but this seems less than ideal to me.
The first three of these are pretty simple, as they are just plain HTML comments and you can use the Haml support for conditional comments:
The last one is a bit different. Breaking it down, what you want is two comments surrounding the
html
opening tag, so the second comment is the first content of thehtml
element. Also you can’t use the Haml syntax for conditional comments, you’ll have to use a literal comment. In Haml this would look like:This will produce HTML like this:
If you wanted you could use the whitespace removal syntax to make the generated HTML more like your example:
Putting it all together:
which produces:
An alternative technique would be to use Haml’s surround helper: