Class: Stache::Handlebars::Handler

Inherits:
Object
  • Object
show all
Includes:
Util.av_template_class(:Handlers)::Compilable
Defined in:
lib/stache/handlebars/handler.rb

Overview

From HAML, thanks a bunch, guys! In Rails 3.1+, template handlers don’t inherit from anything. In <= 3.0, they do. To avoid messy logic figuring this out, we just inherit from whatever the ERB handler does.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(template) ⇒ Object

In Rails 3.1+, #call takes the place of #compile



66
67
68
# File 'lib/stache/handlebars/handler.rb', line 66

def self.call(template)
  new.compile(template)
end

Instance Method Details

#compile(template) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stache/handlebars/handler.rb', line 13

def compile(template)
  handlebars_class = handlebars_class_from_template(template)
  <<-RUBY_CODE
    handlebars = ::#{handlebars_class}.new
    handlebars.view = self

    handlebars.register_helper('helperMissing') do |name, *args|
      meth, *params, options = args

      if handlebars.respond_to?(meth)
        handlebars.send(meth, *params)
      elsif self.respond_to?(meth)
        self.send(meth, *params)
      elsif params.size == 0
        ""
      else
        raise "Could not find property '\#\{meth\}'"
      end
    end

    handlebars.register_helper('yield') do |name, *args|
      content_for(:layout)
    end

    template = handlebars.compile('#{template.source.gsub(/'/, "\\\\'")}');
    vars = {}
    partial_renderer = @view_renderer.send(:_partial_renderer)
    vars.merge!(@_assigns)
    vars.merge!(partial_renderer.instance_variable_get('@locals') || {})
    options = partial_renderer.instance_variable_get('@options')
    vars.merge!(options[:context] || {}) if options

    handlebars.partial_missing do |name|
      search_path = '#{template.virtual_path}'.split("/")[0..-2]
      file = (search_path + [name]).join("/")
      finder = lambda do |partial|
        self.lookup_context.find(file, [], partial, [], {:formats => [:html]})
      end
      template = finder.call(false) rescue finder.call(true)

      if template
        t = handlebars.compile(template.source)
        t.call(vars).html_safe
      else
        raise "Could not find template '\#\{file\}'"
      end
    end

    template.call(vars).html_safe
  RUBY_CODE
end

#handlebars_class_from_template(template) ⇒ Object

suss out a constant name for the given template



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/stache/handlebars/handler.rb', line 71

def handlebars_class_from_template(template)
  const_name = ActiveSupport::Inflector.camelize(template.virtual_path.to_s)
  const_name = "#{Stache.wrapper_module_name}::#{const_name}" if Stache.wrapper_module_name

  begin
    const_name.constantize
  rescue NameError, LoadError => e
    # Only rescue NameError/LoadError concerning our mustache_class
    if e.message.match(/#{const_name}$/)
      Stache::Handlebars::View
    else
      raise e
    end
  end
end