EJS gem in rails 3.1 won't compile JavaScript templates properly

683 Views Asked by At

I am trying to use the EJS gem for templating in rails 3.1. When I require my template in the application.js file

//= require_directory ./templates

The output I get on the client side wraps the template in an anonymous function and namespaces it, but... that's it. This is the generated output I get.

(function() {
  this.JST || (this.JST = {});
  this.JST["templates/index"] = <article class="item <%=type%>">
    <% if (type === "stat") { %>
      <h2>
        <span>70%</span>
        of teens have one or more social network profiles
      </h2>
    <% } else { %>
      <header>
        <a href="/posts/<%=id%>">
          <h3><%=type%></h3>
          <h2><span>- <%=type%></span></h2>
        </a>
      </header>
      <% if (confidential) { %>
        <span class="confidential">Confidential</span>
      <% } %>
      <% if (type === "video" || type === "music") { %>
        <a href="/posts/<%=id%>" class="play">play</a>
      <% } %>
      <a href="/posts/<%=id%>"><img src="<%=image%>" alt="" /></a>
    <% } %>
  </article>;
}).call(this);

I would expect the template to be compiled into a string. That's the experience I've had with Jammit in the past. Do I need to do that manually? Am I missing something?

Thanks in advance,

A

2

There are 2 best solutions below

0
On

Hmm,

Interestingly, installing rails-backbone gem, rather than placing backbone in the app manually, seemed to solve the problem. I also moved the templates into the backbone directory structure. Maybe the ejs gem has some dependency on the backbone gem (unlikely I think)? Or is it something to do with directory nesting levels, or the way asset pipeline includes directories?

Either way, not sure why this is working but it is working none the less. If anyone could serve up an explanation, I'd appreciate it.

0
On

Sprockets wasn't processing your template through EJS because it didn't end in 'ejs'. You need to use an extension ending in ".jst.ejs" with your template files to get them processed in the right order.