Read more

Compiling Javascript template functions with the asset pipeline

Henning Koch
September 16, 2013Software engineer at makandra GmbH

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" }));
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

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 08:23)