module EJS

EJS (Embedded JavaScript) template compiler for Ruby This is a port of Underscore.js' `_.template` function: documentcloud.github.com/underscore/

Constants

JS_ESCAPES
JS_ESCAPE_PATTERN
JS_UNESCAPES
JS_UNESCAPE_PATTERN

Attributes

escape_pattern[RW]
evaluation_pattern[RW]
interpolation_pattern[RW]

Public Class Methods

compile(source, options = {}) click to toggle source

Compiles an EJS template to a JavaScript function. The compiled function takes an optional argument, an object specifying local variables in the template. You can optionally pass the `:evaluation_pattern` and `:interpolation_pattern` options to `compile` if you want to specify a different tag syntax for the template.

EJS.compile("Hello <%= name %>")
# => "function(obj){...}"
# File lib/ejs.rb, line 34
def compile(source, options = {})
  source = source.dup

  js_escape!(source)
  replace_escape_tags!(source, options)
  replace_interpolation_tags!(source, options)
  replace_evaluation_tags!(source, options)
  "function(obj){var __p=[],print=function(){__p.push.apply(__p,arguments);};" +
    "with(obj||{}){__p.push('#{source}');}return __p.join('');}"
end
evaluate(template, locals = {}, options = {}) click to toggle source

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"
# File lib/ejs.rb, line 53
def evaluate(template, locals = {}, options = {})
  require "execjs"
  context = ExecJS.compile("var evaluate = #{compile(template, options)}")
  context.call("evaluate", locals)
end

Protected Class Methods

escape_function() click to toggle source
# File lib/ejs.rb, line 88
def escape_function
  ".replace(/&/g, '&amp;')" +
  ".replace(/</g, '&lt;')" +
  ".replace(/>/g, '&gt;')" +
  ".replace(/\"/g, '&quot;')" +
  ".replace(/'/g, '&#x27;')" +
  ".replace(/\\//g,'&#x2F;')"
end
js_escape!(source) click to toggle source
# File lib/ejs.rb, line 60
def js_escape!(source)
  source.gsub!(JS_ESCAPE_PATTERN) { |match| '\' + JS_ESCAPES[match] }
  source
end
js_unescape!(source) click to toggle source
# File lib/ejs.rb, line 65
def js_unescape!(source)
  source.gsub!(JS_UNESCAPE_PATTERN) { |match| JS_UNESCAPES[match[1..-1]] }
  source
end
replace_escape_tags!(source, options) click to toggle source
# File lib/ejs.rb, line 70
def replace_escape_tags!(source, options)
  source.gsub!(options[:escape_pattern] || escape_pattern) do
    "',(''+#{js_unescape!($1)})#{escape_function},'"
  end
end
replace_evaluation_tags!(source, options) click to toggle source
# File lib/ejs.rb, line 76
def replace_evaluation_tags!(source, options)
  source.gsub!(options[:evaluation_pattern] || evaluation_pattern) do
    "'); #{js_unescape!($1)}; __p.push('"
  end
end
replace_interpolation_tags!(source, options) click to toggle source
# File lib/ejs.rb, line 82
def replace_interpolation_tags!(source, options)
  source.gsub!(options[:interpolation_pattern] || interpolation_pattern) do
    "', #{js_unescape!($1)},'"
  end
end