Skip to content

Instantly share code, notes, and snippets.

@zaius
Created November 20, 2013 21:51
Show Gist options
  • Save zaius/7571733 to your computer and use it in GitHub Desktop.
Save zaius/7571733 to your computer and use it in GitHub Desktop.
Serving ember templates from rails
# Put in config/initializers/handlebars.rb
# Add templates to app/assets/javascripts/templates/whatever.js.handlebars
# Then require from applicaiton.js (or any manifest) as usual. e.g.
# //= require_tree templates
class EmberHandlebars < Tilt::Template
def self.default_mime_type
'application/javascript'
end
def prepare; end
def evaluate scope, locals, &block
path = scope.logical_path.gsub /^templates\//, ''
<<-JS
window.Ember.TEMPLATES["#{path}"] = Ember.Handlebars.compile(#{MultiJson.dump data});
JS
end
end
Sprockets.register_engine '.handlebars', EmberHandlebars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment