Skip to content

Instantly share code, notes, and snippets.

@seven1m
Created May 24, 2011 22:24
Show Gist options
  • Save seven1m/989891 to your computer and use it in GitHub Desktop.
Save seven1m/989891 to your computer and use it in GitHub Desktop.
Render ERB JS templates with Underscore.js and Sprockets and Tilt in Rails 3.1
# creates a global JST object containing all the templates
# 1. install Tilt in your Gemspec
# 2. Paste this in a file at app/assets/javascripts/templates.js.coffee.erb
# 3. `require` the templates file from your main js include file.
# 4. Put your templates at app/views/whatever/my_template.html.jst
window.JST =
<% Dir[Rails.root.join('app/views/**/*.jst')].each do |path| %>
<%= path.match(%r{/([^/]+)\.html\.jst$})[1] %>: _.template(<%= File.read(path).inspect.gsub(/\r?\n/, '') %>)
<% end %>
@masterkain
Copy link

this isn't reloading in development mode. any solution to that?

@seven1m
Copy link
Author

seven1m commented Jun 18, 2011

I think Rails only renders the CoffeeScript to JS whenever the file is changed. That's probably why it won't reload. Not sure how to get around that at the moment. Maybe try touching the templates file as a workaround?

@masterkain
Copy link

Yes, we're doing that but it's proving painful to say the least :)

Cheers

@devth
Copy link

devth commented Jul 12, 2011

Looks like a good fit for guard

@devth
Copy link

devth commented Jul 13, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment