Skip to content

Instantly share code, notes, and snippets.

@buhrmi
Created January 28, 2011 08:48
Show Gist options
  • Save buhrmi/800007 to your computer and use it in GitHub Desktop.
Save buhrmi/800007 to your computer and use it in GitHub Desktop.
A haml filter that let's you call client javascript in ruby, utilizing Rjs
# In config/initializers/haml_filters.rb
module Haml
module Filters
module Client
include Base
def compile(precompiler, text)
return if precompiler.options[:suppress_eval]
precompiler.instance_eval do
push_silent <<-FIRST.gsub("\n", ';') + text + <<-LAST.gsub("\n", ';')
update_page do |page|
page.instance_exec do
FIRST
end
end
LAST
end
end
end
end
end
# In application_helper.rb
def ready_tag string = nil
string = yield if block_given?
javascript_tag '$(function() {'+string+'})'
end
# Your template:
# #testing
# - foo = "Hello from Ruby"
# - ready_tag do
# :client
# alert foo
# Output:
# <div id="testing">
# <script type="text/javascript">
# //<![CDATA[
# $(function() {
# alert("Hello from Ruby");
# })
# //]]>
# </script>
# </div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment