Sha256: d96613a8fa8f961c7cee6a3ad5cb8d25b935c5e0802f92c4123e695202642972

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'tilt'

module HandlebarsAssets
  class TiltHandlebars < Tilt::Template
    def self.default_mime_type
      'application/javascript'
    end

    def evaluate(scope, locals, &block)
      name = basename(scope.logical_path)
      compiled_hbs = Handlebars.precompile data

      if name.starts_with?('_')
        partial_name = name[1..-1].inspect
        <<-PARTIAL
          (function() {
            Handlebars.registerPartial(#{partial_name}, Handlebars.template(#{compiled_hbs}));
          }).call(this);
        PARTIAL
      else
        template_name = scope.logical_path.inspect
        <<-TEMPLATE
          function(context) {
            return HandlebarsTemplates[#{template_name}](context);
          };
          this.HandlebarsTemplates || (this.HandlebarsTemplates = {});
          this.HandlebarsTemplates[#{template_name}] = Handlebars.template(#{compiled_hbs});
        TEMPLATE
      end
    end

    protected

    def basename(path)
      path.gsub(%r{.*/}, '')
    end

    def prepare; end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
handlebars_assets-0.0.2 lib/handlebars_assets/tilt_handlebars.rb