Best way to do conditional body tag in HAML

26 views
Skip to first unread message

Noel_g

unread,
Jun 23, 2009, 4:00:35 PM6/23/09
to Haml
Following up on this compass thread

http://groups.google.com/group/compass-users/browse_thread/thread/fbe8470caccec27b

What would be the best way to implement the conditional body tag in my
HAML layout?

http://paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/

I thought I could do
/[if !IE]
%body
/[endif]

but that doesn't work. On the Compass thread someone mentioned that
it would need to be done in erb.

So would I do

:erb
<!--[if !IE]>--> <body> <!--<![endif]-->

Is this the best way to do this?

scottwb

unread,
Jun 23, 2009, 4:11:53 PM6/23/09
to Haml
I bet somebody has a better way, but here's something I've done before
- capture the haml and then render it with or without the %body based
on your conditional. Not real pretty, but at least the ugliness is
mostly just confined to the top and bottom of the file. Something like
this:

- body = capture_haml do
- ...haml for everything in layout...

- if request.env['HTTP_USER_AGENT'] !~ /msie/i
%body= body
- else
= body

Nathan Weizenbaum

unread,
Jun 23, 2009, 11:45:14 PM6/23/09
to ha...@googlegroups.com
For this sort of thing, I just use a simple string-manipulating helper: http://gist.github.com/134988

Chris Eppstein

unread,
Jun 23, 2009, 11:48:06 PM6/23/09
to ha...@googlegroups.com
That's pretty sweet.

chris

Noel

unread,
Jun 24, 2009, 12:39:43 AM6/24/09
to ha...@googlegroups.com
how do you use that? ...n00b

Bradley Grzesiak

unread,
Jun 24, 2009, 12:44:27 AM6/24/09
to ha...@googlegroups.com
I'm guessing:

conditional_body do
  rest of haml document

Just make sure to put that gist in a helper... most likely in the application helper if you're going to use it everywhere.

:brad
--
Bradley Grzesiak
list...@gmail.com
http://toleoandbeyond.blogspot.com

* You have received an email from my personal account. Please do not divulge this address to any website (eg: evite, shutterfly, etc). I have another address for such uses; please ask me for it.

Chris Eppstein

unread,
Jun 24, 2009, 12:46:30 AM6/24/09
to ha...@googlegroups.com

Like this:

!!!
%html
  %head
    %title This is the tile
  - conditional_body do
    %p
      This is text in the body.
  
-chris

Bradley Grzesiak

unread,
Jun 24, 2009, 12:48:34 AM6/24/09
to ha...@googlegroups.com
right... the hyphen... don't forget that.

*slaps forehead*

nice helper by the way. i look forward to never having to use it because all browsers are standards compliant (in about 40 years). =)

:brad

Noel

unread,
Jun 24, 2009, 12:58:38 AM6/24/09
to ha...@googlegroups.com
thanks, that's cool
Reply all
Reply to author
Forward
0 new messages