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
# 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
           else
             data
           end

  if HandlebarsAssets::Config.ember?
    "window.Ember.TEMPLATES[#{template_path.name}] = Ember.Handlebars.compile(#{source.to_json});"
  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



56
57
58
59
60
# File 'lib/handlebars_assets/tilt_handlebars.rb', line 56

def initialize_engine
  require_template_library 'haml'
rescue LoadError
  # haml not available
end