Created
January 28, 2011 08:48
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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