Skip to content

Commit

Permalink
Support compilation using Ember.HTMLBars.tempalte
Browse files Browse the repository at this point in the history
  • Loading branch information
tricknotes committed Dec 28, 2014
1 parent 13f4d16 commit 8ac4732
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -119,6 +119,7 @@ config files (`config/application.rb`, `config/environments/development.rb`, etc
| `config.handlebars.templates_root` | Sets the root path (under `app/assets/javascripts`) for templates to be looked up in. Default value: `"templates"`. |
| `config.handlebars.templates_path_separator` | The path separator to use for templates. Default value: `'/'`. |
| `config.handlebars.output_type` | Configures the style of output (options are `:amd` and `:global`). Default value: `:global`. |
| `config.handlebars.ember_template` | Default which Ember template type to compile. Valid options: `'Handlebars', `HTMLBars`. Defaults to 'Handlebars`' when `Ember::VERSION` is under 1.10.0, `HTMLBars` when `Ember::VERSION` is over 1.10.0. |

Note:

Expand Down
12 changes: 6 additions & 6 deletions lib/ember/handlebars/template.rb
Expand Up @@ -24,13 +24,13 @@ def evaluate(scope, locals, &block)
if raw
template = precompile_handlebars(template)
else
template = precompile_ember_handlebars(template)
template = precompile_ember_handlebars(template, configuration.ember_template)
end
else
if raw
template = compile_handlebars(data)
else
template = compile_ember_handlebars(template)
template = compile_ember_handlebars(template, configuration.ember_template)
end
end

Expand Down Expand Up @@ -67,12 +67,12 @@ def precompile_handlebars(string)
"Handlebars.template(#{Barber::Precompiler.compile(string)});"
end

def compile_ember_handlebars(string)
"Ember.Handlebars.compile(#{indent(string).inspect});"
def compile_ember_handlebars(string, ember_template = 'Handlebars')
"Ember.#{ember_template}.compile(#{indent(string).inspect});"
end

def precompile_ember_handlebars(string)
"Ember.Handlebars.template(#{Barber::Ember::Precompiler.compile(string)});"
def precompile_ember_handlebars(string, ember_template = 'Handlebars')
"Ember.#{ember_template}.template(#{Barber::Ember::Precompiler.compile(string)});"
end

def mustache_to_handlebars(scope, template)
Expand Down
1 change: 1 addition & 0 deletions lib/ember/rails/engine.rb
Expand Up @@ -11,6 +11,7 @@ class Engine < ::Rails::Engine
config.handlebars.templates_root = "templates"
config.handlebars.templates_path_separator = '/'
config.handlebars.output_type = :global
config.handlebars.ember_template = Ember::VERSION =~ /\A1.[0-9]\./ ? 'Handlebars' : 'HTMLBars'

config.before_initialize do |app|
Sprockets::Engines #force autoloading
Expand Down
4 changes: 2 additions & 2 deletions lib/ember_rails.rb
@@ -1,8 +1,8 @@
require 'rails'
require 'ember/rails/version'
require 'ember/rails/engine'
require 'ember/source'
require 'ember/data/source'
require 'ember/rails/version'
require 'ember/rails/engine'
require 'handlebars/source'

module Ember
Expand Down
38 changes: 32 additions & 6 deletions test/hjstemplate_test.rb
Expand Up @@ -48,6 +48,14 @@ def with_amd_output
handlebars.amd_namespace = old_namespace
end

def with_ember_template(template)
old, handlebars.ember_template = handlebars.ember_template, template

yield
ensure
handlebars.ember_template = old
end

test "should replace separators with templates_path_separator" do
with_template_root("", "-") do
t = Ember::Handlebars::Template.new {}
Expand All @@ -67,7 +75,6 @@ def with_amd_output
test "should strip templates_root with / in it" do
with_template_root("app/templates") do
t = Ember::Handlebars::Template.new {}
#old, handlebars.templates_root = handlebars.templates_root, 'app/templates'
path = t.send(:template_path, 'app/templates/app/example')
assert path == 'app/example', path
end
Expand All @@ -76,7 +83,6 @@ def with_amd_output
test "should strip different template roots" do
with_template_root(["templates", "templates_mobile"]) do
t = Ember::Handlebars::Template.new {}
#old, handlebars.templates_root = handlebars.templates_root, 'app/templates'
path = t.send(:template_path, 'templates/app/example')
assert path == 'app/example', path

Expand All @@ -102,26 +108,26 @@ def with_amd_output
template = Ember::Handlebars::Template.new template_path.to_s
asset = app.assets.attributes_for(template_path)

assert_match /define\('appkit\/templates\/test', \['exports'\], function\(__exports__\)\{ __exports__\['default'\] = Ember\.Handlebars\.template\(/m, template.render(asset)
assert_match /define\('appkit\/templates\/test', \['exports'\], function\(__exports__\)\{ __exports__\['default'\] = Ember\.(?:Handlebars|HTMLBars)\.template\(/m, template.render(asset)
end
end

test "asset pipeline should serve template" do
get "/assets/templates/test.js"
assert_response :success
assert_match /Ember\.TEMPLATES\["test"\] = Ember\.Handlebars\.template\(/m, @response.body
assert_match /Ember\.TEMPLATES\["test"\] = Ember\.(?:Handlebars|HTMLBars)\.template\(/m, @response.body
end

test "asset pipeline should serve bundled application.js" do
get "/assets/application.js"
assert_response :success
assert_match /Ember\.TEMPLATES\["test"\] = Ember\.Handlebars\.template\(/m, @response.body
assert_match /Ember\.TEMPLATES\["test"\] = Ember\.(?:Handlebars|HTMLBars)\.template\(/m, @response.body
end

test "should unbind mustache templates" do
get "/assets/templates/hairy.mustache"
assert_response :success
assert_match /Ember\.TEMPLATES\["hairy(\.mustache)?"\] = Ember\.Handlebars\.template\(/m, @response.body
assert_match /Ember\.TEMPLATES\["hairy(\.mustache)?"\] = Ember\.(?:Handlebars|HTMLBars)\.template\(/m, @response.body
assert_match /function .*unbound|"name":"unbound"/m, @response.body
end

Expand All @@ -131,4 +137,24 @@ def with_amd_output
assert @response.body =~ /; data = data || {};\n|"data":data/, @response.body.inspect
end

test "compile template with Handlebars namespace" do
with_ember_template 'Handlebars' do
template_path = ::Rails.root.join('app/assets/javascripts/templates/test.js.hjs')
template = Ember::Handlebars::Template.new template_path.to_s
asset = app.assets.attributes_for(template_path)

assert_match /Ember\.TEMPLATES\["test(\.js)?"\] = Ember\.Handlebars\.template\(/m, template.render(asset)
end
end

test "compile template with HTMLBars namespace" do
with_ember_template 'HTMLBars' do
template_path = ::Rails.root.join('app/assets/javascripts/templates/test.js.hjs')
template = Ember::Handlebars::Template.new template_path.to_s
asset = app.assets.attributes_for(template_path)

assert_match /Ember\.TEMPLATES\["test(\.js)?"\] = Ember\.HTMLBars\.template\(/m, template.render(asset)
end
end

end

0 comments on commit 8ac4732

Please sign in to comment.