Skip to content

Commit

Permalink
refactor handlebars precompiler
Browse files Browse the repository at this point in the history
* support for raw handlebars precompiler
* fixed a test with a new handlebars
  • Loading branch information
tchak committed Nov 1, 2012
1 parent a634dac commit f2d2cb5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
48 changes: 42 additions & 6 deletions lib/ember/handlebars/template.rb
Expand Up @@ -12,22 +12,58 @@ def self.default_mime_type
def prepare; end def prepare; end


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

if raw
template = data
else else
template = mustache_to_handlebars(scope, data) template = mustache_to_handlebars(scope, data)
end


if configuration.precompile if configuration.precompile
template = Barber::Ember::FilePrecompiler.call(template) if raw
"Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = #{template}\n" template = precompile_handlebars(template)
else
template = precompile_ember_handlebars(template)
end
else
if raw
template = compile_handlebars(data)
else else
"Ember.TEMPLATES[#{template_path(scope.logical_path).inspect}] = Ember.Handlebars.compile(#{indent(template).inspect});\n" template = compile_ember_handlebars(template)
end end
end end

"#{target} = #{template}\n"
end end


private 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) def mustache_to_handlebars(scope, template)
if scope.pathname.to_s =~ /\.mustache\.(handlebars|hjs|hbs)/ if scope.pathname.to_s =~ /\.mustache\.(handlebars|hjs|hbs)/
template.gsub(/\{\{(\w[^\}\}]+)\}\}/){ |x| "{{unbound #{$1}}}" } template.gsub(/\{\{(\w[^\}\}]+)\}\}/){ |x| "{{unbound #{$1}}}" }
Expand Down
2 changes: 0 additions & 2 deletions lib/ember/handlebars/version.rb
@@ -1,5 +1,3 @@
require 'ember/handlebars/source'

module Ember module Ember
module Handlebars module Handlebars
VERSION = "1.0.rc.1" VERSION = "1.0.rc.1"
Expand Down
2 changes: 0 additions & 2 deletions lib/ember/version.rb
@@ -1,5 +1,3 @@
require 'ember/handlebars/source'

module Ember module Ember
VERSION = "1.0-pre.2" VERSION = "1.0-pre.2"
end end
2 changes: 1 addition & 1 deletion test/hjstemplate_test.rb
Expand Up @@ -69,7 +69,7 @@ def with_template_root(root, sep=nil)
test "ensure new lines inside the anon function are persisted" do test "ensure new lines inside the anon function are persisted" do
get "/assets/templates/new_lines.js" get "/assets/templates/new_lines.js"
assert_response :success 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


end end

0 comments on commit f2d2cb5

Please sign in to comment.