Class: HandlebarsAssets::TiltHandlebars

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

Defined Under Namespace

Classes: TemplatePath

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Unindent

#unindent

Class Method Details

.default_mime_typeObject



18
19
20
# File 'lib/handlebars_assets/tilt_handlebars.rb', line 18

def self.default_mime_type
  'application/javascript'
end

Instance Method Details

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



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
# File 'lib/handlebars_assets/tilt_handlebars.rb', line 22

def evaluate(scope, locals, &block)
  template_path = TemplatePath.new(scope)

  source = if template_path.is_haml?
             Haml::Engine.new(data, HandlebarsAssets::Config.haml_options).render
           elsif template_path.is_slim?
             Slim::Template.new(HandlebarsAssets::Config.slim_options) { data }.render
           else
             data
           end

  if HandlebarsAssets::Config.ember?
    "window.Ember.TEMPLATES[#{template_path.name}] = Ember.Handlebars.compile(#{MultiJson.dump source});"
  else
    compiled_hbs = Handlebars.precompile(source, HandlebarsAssets::Config.options)

    template_namespace = HandlebarsAssets::Config.template_namespace

    if template_path.is_partial?
      unindent <<-PARTIAL
        (function() {
          Handlebars.registerPartial(#{template_path.name}, Handlebars.template(#{compiled_hbs}));
        }).call(this);
      PARTIAL
    else
      unindent <<-TEMPLATE
        (function() {
          this.#{template_namespace} || (this.#{template_namespace} = {});
          this.#{template_namespace}[#{template_path.name}] = Handlebars.template(#{compiled_hbs});
          return this.#{template_namespace}[#{template_path.name}];
        }).call(this);
      TEMPLATE
    end
  end
end

#initialize_engineObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/handlebars_assets/tilt_handlebars.rb', line 58

def initialize_engine
  begin
    require 'haml'
  rescue LoadError
    # haml not available
  end
  begin
    require 'slim'
  rescue LoadError
    # slim not available
  end
end