Method: EJS.evaluate

Defined in:
lib/ejs.rb

.evaluate(template, locals = {}, options = {}) ⇒ Object

Evaluates an EJS template with the given local variables and compiler options. You will need the ExecJS (github.com/sstephenson/execjs/) library and a JavaScript runtime available.

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


72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ejs.rb', line 72

def evaluate(template, locals = {}, options = {})
  require "execjs"
  context = ExecJS.compile(<<-JS)
    #{escape_function}
    
    var template = #{compile(template, options)}
    var evaluate = function(locals) {
      return template(locals, escape);
    }
  JS
  context.call("evaluate", locals)
end