Created
December 5, 2010 06:49
-
-
Save wycats/728887 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'v8' | |
class HandlebarsTemplateHandler | |
def self.js | |
Thread.current[:v8_context] ||= begin | |
V8::Context.new do |js| | |
js.load(File.join(Rails.root, 'vendor', 'javascripts', 'handlebars.js')) | |
js.eval("Templates = {}") | |
end | |
end | |
end | |
def self.rails_helper | |
Thread.current[:v8_rails_helper] ||= begin | |
js.eval(%{ | |
(function(helper) { | |
var params = Array.prototype.slice.call(arguments, 1); | |
var av = this.__context__.fallback.actionview; | |
return av[helper].apply(av, params); | |
}) | |
}) | |
end | |
end | |
def self.call(template) | |
# Here, we're sticking the compiled template somewhere in V8 where | |
# we can get back to it | |
js.eval(%{Templates["#{template.identifier}"] = Handlebars.compile(#{template.source.inspect}) }) | |
%{ | |
js = HandlebarsTemplateHandler.js | |
fallback = { :actionview => self, :rails => ::HandlebarsTemplateHandler.rails_helper } | |
js.eval("Templates['#{template.identifier}']").call(assigns, fallback) | |
} | |
end | |
end | |
ActionView::Template.register_template_handler(:hbs, HandlebarsTemplateHandler) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment