Class: HandlebarsAssets::TiltHandlebars

Inherits:
Tilt::Template
  • Object
show all
Defined in:
lib/handlebars_assets/tilt_handlebars.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_mime_typeObject



5
6
7
# File 'lib/handlebars_assets/tilt_handlebars.rb', line 5

def self.default_mime_type
  'application/javascript'
end

Instance Method Details

#evaluate(scope, locals, &block) ⇒ Object



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

def evaluate(scope, locals, &block)
  name = basename(scope.logical_path)
  compiled_hbs = Handlebars.precompile(data)

  if name.start_with?('_')
    partial_name = name[1..-1].inspect
    <<-PARTIAL
      (function() {
        Handlebars.registerPartial(#{partial_name}, Handlebars.template(#{compiled_hbs}));
      }).call(this);
    PARTIAL
  else
    template_name = scope.logical_path.inspect
    <<-TEMPLATE
      (function() {
        this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
        this.HandlebarsTemplates[#{template_name}] = Handlebars.template(#{compiled_hbs});
        return HandlebarsTemplates[#{template_name}];
      }).call(this);
    TEMPLATE
  end
end