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

~> 1.4.0
~> 1.7.7

Runtime

>= 0
>= 2.1.3
 Project Readme

THIS PROJECT IS UNMAINTAINED

Feel free to fork and start your own version. :)

Haml Underscore Template Compiler

This project is an addendum to the Underscore Template Compiler built by Jean-Sebastien Ney. You can follow my fork link back to his project if you are interested in the original code. This code will no longer be following that repository.

Purpose

Say that you're using BackboneJS, UnderscoreJS, and jQuery. A lot of the time you end up putting templates directly in to the HTML, and then go after them utilizing the jQuery $('#template-for-things').html() functionality, rather than having them directly included within your asset pipeline. That is not an insignificant operation for the browser to have to go running after, every time a page boots up. This functionality already exists in sprockets implemented as EJS and the JST function from the browser side. Additionally, if you're using haml and have already built a lot of your templates in that matter, it is a non-trivial task to convert those all over to HTML. And really, why would you want to? HAML is a lot easier to write than HTML, and it's a lot easier to reason about. Thus, this project.

Backbone.js | Underscore.js | ejs | sprockets | JST |

Just add this to your Gemfile :

gem 'haml-underscore-template'

Example :

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

// 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 Christopher Rueber

(most code originally written by Jean-Sebastien Ney, and a lot of framework code by @sstephenson)

The MIT License