Sha256: f1d50e745d192c869edfc741975365e3b64fde392da4e025b98238dbd4dcf01c

Contents?: true

Size: 1016 Bytes

Versions: 2

Compression:

Stored size: 1016 Bytes

Contents

require 'tilt'
require 'execjs'

module ShtRails
  class Tilt < Tilt::Template
    self.default_mime_type = 'application/javascript'

    def prepare
      @namespace = "this.#{ShtRails.template_namespace}"
    end

    attr_reader :namespace

    def precompile(template)
      @context ||= begin
        handlebars_path = File.expand_path("../../../vendor/assets/javascripts/handlebars.js", __FILE__)
        ExecJS.compile File.read(handlebars_path)
      end
      @context.call("Handlebars.precompile", template)
    end

    def evaluate(scope, locals, &block)
      template_key = path_to_key scope
      <<-HandlebarsTemplate
  (function() {
  #{namespace} || (#{namespace} = {});
  Handlebars.partials[#{template_key.inspect}] = #{namespace}[#{template_key.inspect}] = Handlebars.template(#{precompile(data)});
  }).call(this);
      HandlebarsTemplate
    end

    def path_to_key(scope)
      path = scope.logical_path.to_s.split('/')
      path.last.gsub!(/^_/, '')
      path.join('/')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sht_rails-0.2.3 lib/sht_rails/tilt.rb
sht_rails-0.2.2 lib/sht_rails/tilt.rb