Rails javascript templates helper
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
Gemfile
README.md
Rakefile
templates_for.gemspec

README.md

Templates For

If we have a view with a Javascript application on it our directory structure would look like this:

app/templates/widgets/_nav.html.erb
app/templates/widgets/_details.html.erb

app/views/widgets/show.html.erb

In the details template we might have:

<div class="details">
  <p>{{name}}</p>
</nav>

Gemfile

gem 'templates_for', '~> 0.0.1'

Controller

WidgetController < ActionController::Base

  templates_for :show, %( nav details ) 
 
  def show

  end
end

View

<%= templates_for_view %>

Javascript

var nav     = JST['nav'];
var details = JST['details'];

el = document.getElementById('my_elem');
el.innerHTML = Mustache.render(details, {name: "Bob"});

Other libs

eco (recommended): https://github.com/sstephenson/eco

underscore.js: http://documentcloud.github.com/underscore/#template

mustach.js: https://github.com/janl/mustache.js