Class: Handlebars::TemplateHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/handlebars-rails/template_handler.rb

Class Method Summary collapse

Class Method Details

.call(template) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/handlebars-rails/template_handler.rb', line 8

def self.call(template)
  %{
    handler = ::Handlebars::TemplateHandler
    handlebars = handler.handlebars
    handler.with_view(self) do
      handlebars.compile(#{template.source.inspect}).call(assigns).force_encoding(Encoding.default_external).html_safe
    end
  }
end

.dataObject



45
46
47
# File 'lib/handlebars-rails/template_handler.rb', line 45

def self.data
  @context['rails']
end

.handlebarsObject



4
5
6
# File 'lib/handlebars-rails/template_handler.rb', line 4

def self.handlebars
  @context ||= new_context
end

.new_contextObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/handlebars-rails/template_handler.rb', line 26

def self.new_context
  Handlebars::Context.new.tap do |context|
    context['rails'] = {}
    context.partial_missing do |name|
      lookup_context = data['view'].lookup_context
      prefixes = lookup_context.prefixes.dup
      prefixes.push ''
      partial = lookup_context.find(name, prefixes, true)
      lambda do |this, context|
        if partial.handler == self
          handlebars.compile(partial.source).call(context)
        else
          data['view'].render :partial => name, :locals => context
        end
      end
    end
  end
end

.with_view(view) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/handlebars-rails/template_handler.rb', line 18

def self.with_view(view)
  original_view = data['view']
  data['view'] = view
  yield
ensure
  data['view'] = original_view
end