Monkey Patch!

4 views
Skip to first unread message

Les Hill

unread,
Feb 22, 2008, 10:09:10 AM2/22/08
to rub...@googlegroups.com
So after missing the wisdom[1] of this:

http://xkcd.com/386/

And responding to this:

http://b.lesseverything.com/2008/2/19/haml-doesn-t-like-javascript

I went to bed.

This morning I wrote my first rails (really Haml) Monkey patch[2].
This adds some of what Steve wanted: undisturbed inline javascript
with variable interpolation. I am not sure what else he wanted as I
did not read his blog carefully :)

Throw this into lib/inline_javascript.rb
---
module Haml
module Filters
class InlineJavascript
HEAD =<<EOH
<script type="text/javascript">
//<![CDATA[
EOH

FOOT =<<EOF
//]]>
</script>
EOF

def initialize(text)
@text = HEAD + text + FOOT
end

def render
@text
end
end
end

module Precompiler
def close_filtered(filter)
@flat_spaces = -1
filtered = filter.new(@filter_buffer).render

if filter == Haml::Filters::Preserve
push_silent("_hamlout.buffer << #{filtered.dump} << \"\\n\";")
elsif filter == Haml::Filters::InlineJavascript
# suppress eval option does not apply to us
flush_merged_text
js = unescape_interpolation(filtered)
@precompiled << "_hamlout.buffer << #{js};"
else
push_text(filtered.rstrip.gsub("\n", "\n#{' ' * @output_tabs}"))
end

@filter_buffer = nil
@template_tabs -= 1
end
end
end
---

Throw this at the bottom of your environment.rb[3]

---
require 'inline_javascript'

Haml::Template.options = {
:filters => {
'inline_javascript' => Haml::Filters::InlineJavascript
}
}
---

And then do this in your Haml:

:inline_javascript
function oh_yea() {
alert('Hello' + '#{@message}')
}
%a{ :href =>"javascript:oh_yea()" } Oh Yea!

[1] It's turtles all the way down.
[2] You do not want to know what my subconscious originally thought up
as the way to do this -- that was twisted.
[3] From here http://groups.google.com/group/haml/msg/2d890cf1ede761ea
, where it all started :)
--
Les Hill
les...@gmail.com

Sandro

unread,
Feb 22, 2008, 10:28:26 AM2/22/08
to RubyJax
Warning: Les cannot be deterred by the phrase "patches welcome."

Awesome job!

I ran into the same haml javascript issue just two days ago so seeing
this patch is quite exciting.
> [3] From herehttp://groups.google.com/group/haml/msg/2d890cf1ede761ea
> , where it all started :)
> --
> Les Hill
> lesh...@gmail.com

Obie Fernandez

unread,
Feb 22, 2008, 10:35:20 AM2/22/08
to rub...@googlegroups.com
Hey Les, please post this information to a blog (or whatever) so that
I can link to it asap... This is a very nice hack for Haml fans.

--
Obie Fernandez
http://obiefernandez.com

Les Hill

unread,
Feb 22, 2008, 9:17:59 PM2/22/08
to rub...@googlegroups.com
Since I have no blog, I decided to make a patch and submit it to the
Haml folks directly. This gave me the 'opportunity' to get git going.
Check. For now, I am sticking to svn :)

--
Les Hill
les...@gmail.com

Les Hill

unread,
Feb 25, 2008, 7:15:34 AM2/25/08
to rub...@googlegroups.com
On Fri, Feb 22, 2008 at 9:17 PM, Les Hill <les...@gmail.com> wrote:
> Since I have no blog, I decided to make a patch and submit it to the
> Haml folks directly. This gave me the 'opportunity' to get git going.
> Check. For now, I am sticking to svn :)

Update:

Nathan Weizenbaum took the time to put in a more comprehensive fix to
expose the interpolation functionality to all filters -- look for it
in an upcoming release.

--
Les Hill
les...@gmail.com

Reply all
Reply to author
Forward
0 new messages