The asset pipeline (which is actually backed by
sprockets
Show archive.org snapshot
) has a nice feature where templates ending in .jst
are compiled into Javascript template functions. These templates can be rendered by calling JST['path/to/template'](template: 'variables')
:
<!-- templates/hello.jst.ejs -->
<div>Hello, <span><%= name %></span>!</div>
// application.js
//= require templates/hello
$("#hello").html(JST["templates/hello"]({ name: "Sam" }));
Whatever is in the <% ... %>
is evaluated in Javascript. You can also use CoffeeScript here:
Sprockets supports two JavaScript template languages: EJS, for embedded JavaScript, and Eco, for embedded CoffeeScript. Both languages use the familiar
<% … %>
syntax for embedding logic in templates.
This allows you to e. g. write templates for Backbone views using Haml.
Posted by Henning Koch to makandra dev (2013-09-16 06:23)