Sha256: 35dbc1983043a454269170b8308193934be9b338dff016500367b993efd64d64

Contents?: true

Size: 1.01 KB

Versions: 6

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

6 entries across 6 versions & 1 rubygems

Version Path
handlebars_assets-0.1.4 lib/handlebars_assets/tilt_handlebars.rb
handlebars_assets-0.1.3 lib/handlebars_assets/tilt_handlebars.rb
handlebars_assets-0.1.2 lib/handlebars_assets/tilt_handlebars.rb
handlebars_assets-0.1.1 lib/handlebars_assets/tilt_handlebars.rb
handlebars_assets-0.1.0 lib/handlebars_assets/tilt_handlebars.rb
handlebars_assets-0.0.3 lib/handlebars_assets/tilt_handlebars.rb