No commit activity in last 3 years
No release in over 3 years
Compile and evaluate underscore templates from Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 0.4

Runtime

~> 2.1.3
 Project Readme

Underscore templates compiler for Ruby

Imagine, you've got a rails project using Backbone.js and just want to use the Underscore.js's _.template function without having an anonymous function like ejs does. underscore-template allows you to use sprockets's' JST templating directly with _.template function

Just add this to your Gemfile :

gem 'underscore-template'

Example :

<!-- templates/hello.jst._ -->
<div>Hello, <span><%= name %></span>!</div>

// application.js
//= require templates/hello
$("#hello").html(JST["templates/hello"]({ name: "Sam" }));

Invoke the function in a JavaScript environment to produce a string value. You can pass an optional object specifying local variables for template evaluation.

The underscore template tag syntax is as follows:

  • <% ... %> silently evaluates the statement inside the tags.
  • <%= ... %> evaluates the expression inside the tags and inserts its string value into the template output.
  • <%- ... %> behaves like <%= ... %> but HTML-escapes its output.

If you have the ExecJS library and a suitable JavaScript runtime installed, you can pass a template and an optional hash of local variables to UnderscoreTemplate.evaluate:

Underscore::Template.evaluate("Hello <%= name %>", :name => "world")
# => "Hello world"

© 2012 Jean-Sébastien Ney

(actually most of credits goes to @sstephenson)

Released under the MIT license