Skip to content

Add support for jQuery templates #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backbone-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Gem::Specification.new do |s|

s.summary = "Use backbone.js with rails 3.1"
s.description = "quickly setup backbone.js for use with rails 3.1"
s.files = Dir["lib/**/*"] + Dir["vendor/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"]
s.files = Dir["lib/**/*"] + Dir["vendor/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
s.version = "0.0.1"

s.add_development_dependency "bundler", "~> 1.0.0"
s.add_development_dependency "rails", "~> 3.1"
s.add_development_dependency "railties", "~> 3.1"

s.require_paths = ['lib']
end
1 change: 1 addition & 0 deletions lib/backbone-rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Engine < ::Rails::Engine

config.after_initialize do |app|
app.assets.engines.register '.jst', Tilt::UnderscoreTemplate
app.assets.engines.register '.jqt', Tilt::JqueryTemplate
end
end

Expand Down
12 changes: 12 additions & 0 deletions lib/backbone-rails/jquery.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Jquery
extend ActionView::Helpers::JavaScriptHelper

JST_START = "(function(){"
JST_END = "}).call(this);"

def self.render(name, data)
template = "jQuery.template(\"#{name}\", \"#{escape_javascript data}\");"
[JST_START, template, JST_END].join("\n")
end

end
18 changes: 18 additions & 0 deletions lib/backbone-rails/tilt/jquery_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'tilt/template'

module Tilt
class JqueryTemplate < Template
self.default_mime_type = 'text/x-jquery-tmpl'

def initialize_engine
require_template_library 'backbone-rails/jquery'
end

def prepare; end

def evaluate(scope, locals, &block)
Jquery.render(name, data)
end

end
end