EJS (Embedded JavaScript) template compiler for Ruby
Latest commit 674a334 Jun 8, 2012 @sstephenson ruby-ejs 1.1.1
Failed to load latest commit information.
lib Fix JS unescape pattern Jun 7, 2012
test Unescape javscript code just like underscore.js do Jun 7, 2012
LICENSE Initial commit May 17, 2011
README.md ruby-ejs 1.1.0 Jun 1, 2012
Rakefile Initial commit May 17, 2011
ejs.gemspec ruby-ejs 1.1.1 Jun 7, 2012


EJS (Embedded JavaScript) template compiler for Ruby

EJS templates embed JavaScript code inside <% ... %> tags, much like ERB. This library is a port of Underscore.js's _.template function to Ruby, and strives to maintain the same syntax and semantics.

Pass an EJS template to EJS.compile to generate a JavaScript function:

EJS.compile("Hello <%= name %>")
# => "function(obj){...}"

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 EJS 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 EJS.evaluate:

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

© 2012 Sam Stephenson

Released under the MIT license