Class: HandlebarsAssets::HandlebarsRenderer

Inherits:
Object
  • 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

Constructor Details

#initialize(options) ⇒ HandlebarsRenderer

Returns a new instance of HandlebarsRenderer.



99
100
101
102
# File 'lib/handlebars_assets/handlebars_template.rb', line 99

def initialize(options)
  self.class.initialize_engine
  @template_path = TemplatePath.new(options[:path])
end

Class Method Details

.initialize_engineObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/handlebars_assets/handlebars_template.rb', line 82

def self.initialize_engine
  return if @initialized

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

  @initialized = true
end

Instance Method Details

#choose_engine(data) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/handlebars_assets/handlebars_template.rb', line 104

def choose_engine(data)
  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
    NoOpEngine.new(data)
  end
end

#compile(source) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/handlebars_assets/handlebars_template.rb', line 114

def compile(source)
  # 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

#compile_default(source) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/handlebars_assets/handlebars_template.rb', line 132

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



128
129
130
# File 'lib/handlebars_assets/handlebars_template.rb', line 128

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