Take the 2-minute tour ×

I'm a rails developer trying to learn Backbone and then I ran into this problem: since Underscore templates include symbols like <%=%>, I guess templates can't be included into erb files, so is it okay to have a rails partial for every single template? And what extension should it be?

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

You can escape the erb symbols by using two % in the opening tag, and put your backbone templates in the rails views:

<script type='text/template' id="my-template'>
  <%%= name %>

will output the following in your page:

  <%= name %>

Putting your Backbone templates directly in your rails views is IMHO the best option when you're trying to learn. You're already wrestling with the new concepts, no need to add another hurdle.

share|improve this answer
Great tip, I didn't know that. Indeed I use jst which are cleanly stored in a tree inside assets –  apneadiving Nov 15 '11 at 18:27
I was using custom regex for underscore templates. Thanks for the two % tip. –  Agent47DarkSoul Sep 25 '13 at 11:17
add comment

Starting with Rails 3.1, it provides two things that make working with Backbone templates a little easier: the asset pipeline, and automatic JST (JavaScript Template) compilation.

Create a directory in your app/assets folder called templates. This directory will automatically be picked up by the asset pipeline.

Next, name the files in that directory with an extension of jst and the type of template you are creating ejs (embedded javascript). You can even nest them in directories. For example:


The asset pipeline also allows you to use other templating languages like embedded coffeescript, mustache, handlebars, etc. by simply changing the file extension (and including any necessary gems).

Now to reference your JST templates in your Backbone views, simply use the path to the filename:

var Bookmark = Backbone.View.extend({
  template: JST['bookmarks/show'],
  render: function() {
    return this;

You may need to add this line to your application.js:

// require_tree ../templates

Here's a nice article which explains all of this in a little more detail: http://www.bigjason.com/blog/precompiled-javascript-templates-rails-3-1

share|improve this answer
add comment

Where should you put your Backbone templates? I'd say nowhere. I believe that in most Rails applications, the server should be responsible for all rendering of HTML, while the client-side JavaScript should just be responsible for inserting that rendered HTML into the DOM. Among other things, this makes I18n easier.

The exception would be if Rails is simply being used as a lightweight backend for an application that runs mostly on the client side (though in that case, you might want to use Sinatra or something instead). In this case, Rails should probably render nothing, and have the JS do all the rendering.

Notice the underlying principle here. Either the server should be responsible for all rendering, or the client should. Splitting it will make life harder.

share|improve this answer
What about tiny pieces of rendering, like table rows? –  Erik Escobedo Nov 16 '11 at 16:37
Same answer. Don't split the rendering. Or do I misunderstand your question? –  Marnen Laibow-Koser Nov 16 '11 at 16:58
Maybe I misunderstand the answer. Well, in order to use Backbone.js, I have to write templates somewhere. This templates use <%= %> tags and my question was about whether is there a way for making rails render this tags to the client or is there a format I can use for rendering Backbone.js (Underscore.js) templates more easily. –  Erik Escobedo Nov 16 '11 at 21:45
I could be wrong, but I think that it's possible to use Backbone's other features without using its template rendering (or at least, with using its template rendering only to interpolate pre-rendered HTML from Rails). –  Marnen Laibow-Koser Nov 16 '11 at 21:47
add comment

Your Answer


