Sha256: d1ea2ac0238a847c785c7e45b1111a3d7b929783e15e9b8d7f18c2251623dacd

Contents?: true

Size: 1.62 KB

Versions: 5

Compression:

Stored size: 1.62 KB

Contents

require 'sprockets'
require 'sprockets/engines'
require 'ember/handlebars/source'

module Ember
  module Handlebars
    class Template < Tilt::Template
      def self.default_mime_type
        'application/javascript'
      end

      def prepare; end

      def evaluate(scope, locals, &block)
        if scope.pathname.to_s =~ /\.raw\.(handlebars|hjs|hbs)/
          "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Handlebars.compile(#{indent(data).inspect});\n"
        else
          template = mustache_to_handlebars(scope, data)

          if configuration.precompile
            func = Ember::Handlebars.compile(template)
            "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.template(#{func});\n"
          else
            "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.compile(#{indent(template).inspect});\n"
          end
        end
      end

      private

      def mustache_to_handlebars(scope, template)
        if scope.pathname.to_s =~ /\.mustache\.(handlebars|hjs|hbs)/
          template.gsub(/\{\{(\w[^\}\}]+)\}\}/){ |x| "{{unbound #{$1}}}" }
        else
          template
        end
      end

      def template_path(path)
        root = configuration.templates_root

        unless root.blank?
          path.gsub!(/^#{Regexp.quote(root)}\/?/, '')
        end

        path = path.split('/')

        path.join(configuration.templates_path_separator)
      end

      def configuration
        ::Rails.configuration.handlebars
      end

      def indent(string)
        string.gsub(/$(.)/m, "\\1  ").strip
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
ember-rails-lite-0.8.0 lib/ember/handlebars/template.rb
ember-rails-0.8.0 lib/ember/handlebars/template.rb
ember-rails-0.7.0 lib/ember/handlebars/template.rb
ember-rails-0.6.0 lib/ember/handlebars/template.rb
ember-rails-0.5.0 lib/ember/handlebars/template.rb