Project

ruby-ejs

0.0
No commit activity in last 3 years
No release in over 3 years
Compile EJS (Embedded JavaScript) templates in Ruby.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

EJS (Embedded JavaScript) template compiler for Ruby Circle CI

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 a evalation tag (<%=) ends with an open function, the function return a compiled template. For example:

<% formTag = function(template) { return '<form>\n'+template()+'\n</form>'; } %>

<%= formTag(function () { %>
  <input type="submit" />
<% }) %>

generates:

<form>
  <input type="submit" />
</form>

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