Class: HandlebarsAssets::HandlebarsTemplate

Inherits:
Tilt::Template
  • Object
show all
Includes:
Unindent
Defined in:
lib/handlebars_assets/handlebars_template.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



19
20
21
# File 'lib/handlebars_assets/handlebars_template.rb', line 19

def self.default_mime_type
  'application/javascript'
end

Instance Method Details

#compile_default(source) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/handlebars_assets/handlebars_template.rb', line 73

def compile_default(source)
  template =
    if HandlebarsAssets::Config.precompile
      compiled_hbs = Handlebars.precompile(source, HandlebarsAssets::Config.options)
      "Handlebars.template(#{compiled_hbs})"
    else
      "Handlebars.compile(#{JSON.dump(source)})"
    end

  template_namespace = HandlebarsAssets::Config.template_namespace

  if HandlebarsAssets::Config.amd?
    handlebars_amd_path = HandlebarsAssets::Config.handlebars_amd_path
    if HandlebarsAssets::Config.amd_with_template_namespace
      if @template_path.is_partial?
        unindent <<-PARTIAL
          define(['#{handlebars_amd_path}'],function(Handlebars){
            var t = #{template};
            Handlebars.registerPartial(#{@template_path.name}, t);
            return t;
          ;})
        PARTIAL
      else
        unindent <<-TEMPLATE
          define(['#{handlebars_amd_path}'],function(Handlebars){
            return #{template};
          });
        TEMPLATE
      end
    else
      if @template_path.is_partial?
        unindent <<-PARTIAL
          define(['#{handlebars_amd_path}'],function(Handlebars){
            var t = #{template};
            Handlebars.registerPartial(#{@template_path.name}, t);
            return t;
          ;})
        PARTIAL
      else
        unindent <<-TEMPLATE
          define(['#{handlebars_amd_path}'],function(Handlebars){
            this.#{template_namespace} || (this.#{template_namespace} = {});
            this.#{template_namespace}[#{@template_path.name}] = #{template};
            return this.#{template_namespace}[#{@template_path.name}];
          });
        TEMPLATE
      end
    end
  else
    if @template_path.is_partial?
      unindent <<-PARTIAL
        (function() {
          Handlebars.registerPartial(#{@template_path.name}, #{template});
        }).call(this);
      PARTIAL
    else
      unindent <<-TEMPLATE
        (function() {
          this.#{template_namespace} || (this.#{template_namespace} = {});
          this.#{template_namespace}[#{@template_path.name}] = #{template};
          return this.#{template_namespace}[#{@template_path.name}];
        }).call(this);
      TEMPLATE
    end
  end
end

#compile_ember(source) ⇒ Object



69
70
71
# File 'lib/handlebars_assets/handlebars_template.rb', line 69

def compile_ember(source)
  "window.Ember.TEMPLATES[#{@template_path.name}] = Ember.Handlebars.compile(#{JSON.dump(source)});"
end

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



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/handlebars_assets/handlebars_template.rb', line 48

def evaluate(scope, locals, &block)
  source =
    if @engine
      @engine.render(scope, locals, &block)
    else
      data
    end

  # remove trailing \n on file, for some reason the directives pipeline adds this
  source.chomp!($/)

  # handle the case of multiple frameworks combined with ember
  # DEFER: use extension setup for ember
  if (HandlebarsAssets::Config.multiple_frameworks? && @template_path.is_ember?) ||
     (HandlebarsAssets::Config.ember? && !HandlebarsAssets::Config.multiple_frameworks?)
    compile_ember(source)
  else
    compile_default(source)
  end
end

#initialize_engineObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/handlebars_assets/handlebars_template.rb', line 23

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

#prepareObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/handlebars_assets/handlebars_template.rb', line 36

def prepare
  @template_path = TemplatePath.new(@file)
  @engine =
    if @template_path.is_haml?
      Haml::Engine.new(data, HandlebarsAssets::Config.haml_options)
    elsif @template_path.is_slim?
      Slim::Template.new(HandlebarsAssets::Config.slim_options) { data }
    else
      nil
    end
end