Skip to content
Browse files

Initial pass on serving up handlebars templates.

  • Loading branch information...
1 parent 85b770e commit 99a8e6fc5c1e287d8c468f35a54f3a93fc0accd4 @gamepoet committed Feb 29, 2012
Showing with 58 additions and 0 deletions.
  1. +44 −0 lib/sinatra/ember.rb
  2. +14 −0 sinatra-ember.gemspec
View
44 lib/sinatra/ember.rb
@@ -0,0 +1,44 @@
+module Sinatra
+ module Ember
+ def self.version
+ "0.0.1"
+ end
+
+ def self.registered(app)
+ app.extend ClassMethods
+ end
+
+ module ClassMethods
+ # serves ember handlebars templates at the given path
+ def serve_templates(path)
+ get path do
+ content_type :js
+
+ # find all the template files
+ template_files = {}
+ Dir.chdir(settings.views) do
+ Dir['**/*.hbs'].map do |filename|
+ filename =~ /^(.*)\.hbs$/
+ name = $1
+ template_files[name] = File.join(settings.views, filename)
+ end
+ end
+
+ # build up the javascript
+ templates = template_files.map do |(name, file)|
+ content = File.read(file)
+ "Ember.TEMPLATES[#{name.inspect}] = Ember.Handlebars.compile(#{content.inspect});"
+ end
+
+ # wrap it up in a closure
+ output = %{
+ (function() {
+ #{templates.join("\n")}
+ })();
+ }
+ output.strip.gsub(/^ {12}/, '')
+ end
+ end
+ end
+ end
+end
View
14 sinatra-ember.gemspec
@@ -0,0 +1,14 @@
+require './lib/sinatra/ember'
+Gem::Specification.new do |s|
+ s.name = 'sinatra-ember'
+ s.version = Sinatra::Ember.version
+ s.summary = 'Helpers for serving an Ember.js app from Sinatra.'
+ s.description = 'Serves Ember Handlebars pages.'
+ s.authors = ['Ben Scott']
+ s.email = ['gamepoet@gmail.com']
+ s.homepage = 'http://github.com/gamepoet/sinatra-ember'
+ s.files = `git ls-files`.strip.split("\n")
+ s.executables = Dir['bin/*'].map { |f| File.basename(f) }
+
+ s.add_dependency 'sinatra'
+end

0 comments on commit 99a8e6f

Please sign in to comment.
Something went wrong with that request. Please try again.