Permalink
Browse files

refactor handlebars precompiler

* support for raw handlebars precompiler
* fixed a test with a new handlebars
  • Loading branch information...
1 parent a634dac commit f2d2cb57acabb9f9f07cb0dc7a01ac619b8cffcf @tchak tchak committed Nov 1, 2012
Showing with 43 additions and 11 deletions.
  1. +42 −6 lib/ember/handlebars/template.rb
  2. +0 −2 lib/ember/handlebars/version.rb
  3. +0 −2 lib/ember/version.rb
  4. +1 −1 test/hjstemplate_test.rb
View
48 lib/ember/handlebars/template.rb
@@ -12,22 +12,58 @@ def self.default_mime_type
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
- template = Barber::Ember::FilePrecompiler.call(template)
- "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = #{template}\n"
+ if configuration.precompile
+ if raw
+ template = precompile_handlebars(template)
+ else
+ template = precompile_ember_handlebars(template)
+ end
+ else
+ if raw
+ template = compile_handlebars(data)
else
- "Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.compile(#{indent(template).inspect});\n"
+ 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}}}" }
View
2 lib/ember/handlebars/version.rb
@@ -1,5 +1,3 @@
-require 'ember/handlebars/source'
-
module Ember
module Handlebars
VERSION = "1.0.rc.1"
View
2 lib/ember/version.rb
@@ -1,5 +1,3 @@
-require 'ember/handlebars/source'
-
module Ember
VERSION = "1.0-pre.2"
end
View
2 test/hjstemplate_test.rb
@@ -69,7 +69,7 @@ def with_template_root(root, sep=nil)
test "ensure new lines inside the anon function are persisted" do
get "/assets/templates/new_lines.js"
assert_response :success
- assert @response.body.include?("helpers;\n"), @response.body.inspect
+ assert @response.body.include?("helpers; data = data || {};\n"), @response.body.inspect
end
end

0 comments on commit f2d2cb5

Please sign in to comment.