Script in Haml

1,367 views
Skip to first unread message

s.ross

unread,
Dec 24, 2006, 9:14:19 PM12/24/06
to ha...@googlegroups.com
I understand that styles and scripts are discouraged in Haml, but
what about those cases where it really shouldn't be avoided. To wit:

<% if controller.user? %>
<script src="http://www.google-analytics.com/urchin.js" type="text/
javascript">
</script>
<script type="text/javascript">
_uacct = "UA-999999-9";
urchinTracker();
</script>
<% end %>

I love my google analytics, but to make them work, I have to embed
this stuff in my code. I could code it up as a partial in rhtml but
is that the right answer? Has anyone else thought this through and
come up with a good answer?

Thanks,

Steve

Nex3

unread,
Dec 24, 2006, 9:58:32 PM12/24/06
to ha...@googlegroups.com
I'm not understanding... why not just embed it in Haml?

%script{:type=>"text/javascript}
_uacct = "UA-999999-9";
urchinTracker();

Am I missing something obvious?

- Nathan

s.ross

unread,
Dec 25, 2006, 1:42:11 AM12/25/06
to ha...@googlegroups.com
At one point Haml used to gobble up script and style stuff. This may
be a poor example because it simply refers to external script files.
What about embedding *real scripts*? I've had problems with this in
the past where I wanted just some little of page-specific Javascript
and it didn't play well with Haml.

Perhaps the better question is: Are there limitations regarding what
js constructs work well with Haml?

Steve

Nex3

unread,
Dec 25, 2006, 2:43:20 AM12/25/06
to ha...@googlegroups.com
There is the possibility that a script with a Haml-significant character
(like ".", "%", "/", or whatever) could have some unintended
consequences. To avoid this, simply put a backslash at the beginning of
the line and continue on as normal. For instance,

%style
\.angry { font-weight: bold; color: #f00; }

would output

<style>
.angry { font-weight: bold; color: #f00; }
</style>

So it's possible, it's just hard to do... which is Haml's policy for
stylistically-incorrect things.

- Nathan

Spongy

unread,
Jan 22, 2007, 7:59:17 AM1/22/07
to Haml
I don't know how worth it it would be but could another operator be
added perhaps. I know you probably dont want to pollute the operator
space too much but maybe something like

%style !


.angry { font-weight: bold; color: #f00; }

So the ! would tell the parser that the next part was explicit perhaps
and to ignore other operators until the white space changes. The use of
! might not be the best choice as its currently in use of doctype
stuff. Just an idea, possibly not all that well thought out ;)

- Geoff

Nathan Weizenbaum

unread,
Jan 22, 2007, 8:14:00 AM1/22/07
to ha...@googlegroups.com
This will be effectively available as of v1.5, with filters (poke around
the group a bit for more discussion of these, or check out the trunk
documentation). There will be a plain-text filter, that will simply
cause Haml to stop parsing. So, for instance, you could write

%style
:plain


.angry { font-weight: bold; color: #f00; }

- Nathan

Spongy

unread,
Jan 22, 2007, 8:29:00 AM1/22/07
to Haml
Ah, that sounds nice. So is it at the moment better to be playing
around the 1.4 dev branch or the stable version?

- Geoff

Nathan Weizenbaum

unread,
Jan 22, 2007, 8:59:26 AM1/22/07
to ha...@googlegroups.com
The stable branch is just that: stable. The development branch could
have bugs that we don't know about, and might be broken from time to
time (although if you don't update more than once a day, this shouldn't
be a problem). However, Haml is rather stable all the time, and at the
moment there's not a lot of difference between Haml in 1.4 and 1.0
(aside from filters, we've been almost entirely focusing on Sass). I
think there are plenty of people using 1.4 happily. So, if you want the
functionality, go ahead and use it. We'd appreciate your feedback.

- Nathan

Hampton

unread,
Jan 22, 2007, 1:20:18 PM1/22/07
to ha...@googlegroups.com
Or, you could build a Css filter that would wrap it in the right tags.

:css


.angry { font-weight: bold; color: #f00; }

Or even a sass filter.

:sass


.angry
:font-weight bold
:color: #f00

That would output...

<style>


.angry { font-weight: bold; color: #f00; }

</style>

The cool parts about filters are.... you can define your own language
within a Haml template!

-hampton.

Nathan Weizenbaum

unread,
Jan 22, 2007, 2:48:24 PM1/22/07
to ha...@googlegroups.com
Actually, there's already a built-in :sass filter, which does just that :D.

- Nathan

Hampton

unread,
Jan 22, 2007, 3:07:59 PM1/22/07
to ha...@googlegroups.com
I should read the changelog more.
Reply all
Reply to author
Forward
0 new messages