Class: HandlebarWax::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/handlebar_wax/template_handler.rb

Class Method Summary collapse

Class Method Details

.call(template) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/handlebar_wax/template_handler.rb', line 31

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}) })
  
  if defined?(Encoding) 
    %{
      js = ::HandlebarWax::TemplateHandler.js
      js['actionview'] = self
      js.eval("Templates['#{template.identifier}']").call(assigns.merge(locals)).force_encoding(Encoding.default_external)
    }
  else 
    %{
      js = ::HandlebarWax::TemplateHandler.js
      js['actionview'] = self
      js.eval("Templates['#{template.identifier}']").call(assigns.merge(locals))
    }
  end 
end

.jsObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/handlebar_wax/template_handler.rb', line 7

def self.js
  handlebars = File.join(Rails.root, "vendor", "javascripts", "handlebars.js")

  unless File.exists?(handlebars)
    raise "Could not find handlebars.js. Please copy it to #{handlebars}"
  end

  Thread.current[:v8_context] ||= begin
    V8::Context.new do |js|
      js.load(handlebars)
      js.eval("Templates = {}")

      js["puts"] = method(:puts)

      js.eval(%{
        Handlebars.registerHelper('helperMissing', function(helper) {
          var params = Array.prototype.slice.call(arguments, 1);
          return actionview[helper].apply(actionview, params);
        })
      })
    end
  end
end