class Mutton::HandlebarsTemplateConverter

convert hbs files into javascript for the asset pipeline

Public Class Methods

default_mime_type() click to toggle source
# File lib/mutton/handlebars_template_converter.rb, line 7
def self.default_mime_type
  'application/javascript'
end

Public Instance Methods

evaluate(scope, _locals, &_block) click to toggle source

called by sprockets when compiling

# File lib/mutton/handlebars_template_converter.rb, line 20
def evaluate(scope, _locals, &_block)
  timing = {}
  ActiveSupport::Notifications.instrument 'evaluate_asset.Mutton', timing do
    start_time = Time.zone.now
    # clients require precompiled templates for optimization
    precompiled_template = HandlebarsCompiler.pre_compile(data)
    end_precompile_time = Time.zone.now
    start_write_source = Time.zone.now
    source = write_source(precompiled_template, scope.logical_path, Mutton.template_namespace)
    end_time = Time.zone.now
    precompile_time = ((end_precompile_time - start_time) * 1000).round(0)
    source_time = ((end_time - start_write_source) * 1000).round(0)
    timing[:precompile_time] = precompile_time
    timing[:source_time] = source_time
    source
  end
end
initialize_engine() click to toggle source

no changes implemented

# File lib/mutton/handlebars_template_converter.rb, line 12
def initialize_engine
end
prepare() click to toggle source

no changes implemented

# File lib/mutton/handlebars_template_converter.rb, line 16
def prepare
end
write_source(code, path, namespace) click to toggle source
# File lib/mutton/handlebars_template_converter.rb, line 38
def write_source(code, path, namespace)
  # drop the first bit of path to get rid of root directory
  paths = path.split('/')
  paths.delete_at(0)
  virtual_path = paths.join('/')
  write_template(namespace, virtual_path, code)
end
write_template(namespace, virtual_path, source) click to toggle source
# File lib/mutton/handlebars_template_converter.rb, line 46
    def write_template(namespace, virtual_path, source)
      javascript_code = <<CODE
(function() {
    var template = Handlebars.template,
        templates = this.#{namespace} = this.#{namespace} || {};
    templates['#{virtual_path}'] = template(
    #{source}
    );
    Handlebars.partials = this.#{namespace};
})(this);
//best with a large serving of mutton
CODE
      javascript_code
    end