lib/ember/handlebars/template.rb in ember-rails-lite-0.8.0 vs lib/ember/handlebars/template.rb in ember-rails-lite-0.9.2

- old
+ new

@@ -1,8 +1,8 @@ require 'sprockets' require 'sprockets/engines' -require 'ember/handlebars/source' +require 'barber' module Ember module Handlebars class Template < Tilt::Template def self.default_mime_type @@ -10,26 +10,62 @@ 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" + target = template_target(scope) + raw = handlebars?(scope) + + if raw + template = data else template = mustache_to_handlebars(scope, data) + end - if configuration.precompile - func = Ember::Handlebars.compile(template) - "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.template(#{func});\n" + if configuration.precompile + if raw + template = precompile_handlebars(template) else - "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.compile(#{indent(template).inspect});\n" + template = precompile_ember_handlebars(template) end + else + if raw + template = compile_handlebars(data) + else + template = compile_ember_handlebars(template) + end end + + "#{target} = #{template}\n" end private + def handlebars?(scope) + scope.pathname.to_s =~ /\.raw\.(handlebars|hjs|hbs)/ + end + + def template_target(scope) + "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}]" + end + + def compile_handlebars(string) + "Handlebars.compile(#{indent(string).inspect});" + end + + def precompile_handlebars(string) + Barber::FilePrecompiler.call(string) + end + + def compile_ember_handlebars(string) + "Ember.Handlebars.compile(#{indent(string).inspect});" + end + + def precompile_ember_handlebars(string) + Barber::Ember::FilePrecompiler.call(string) + end + def mustache_to_handlebars(scope, template) if scope.pathname.to_s =~ /\.mustache\.(handlebars|hjs|hbs)/ template.gsub(/\{\{(\w[^\}\}]+)\}\}/){ |x| "{{unbound #{$1}}}" } else template @@ -37,11 +73,17 @@ end def template_path(path) root = configuration.templates_root - unless root.blank? - path.gsub!(/^#{Regexp.quote(root)}\/?/, '') + if root.kind_of? Array + root.each do |root| + path.gsub!(/^#{Regexp.quote(root)}\//, '') + end + else + unless root.empty? + path.gsub!(/^#{Regexp.quote(root)}\/?/, '') + end end path = path.split('/') path.join(configuration.templates_path_separator)