EJS (Embedded JavaScript) template compiler for Ruby This is a port of Underscore.js' `_.template` function: documentcloud.github.com/underscore/
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
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
# File lib/ejs.rb, line 88 def escape_function ".replace(/&/g, '&')" + ".replace(/</g, '<')" + ".replace(/>/g, '>')" + ".replace(/\"/g, '"')" + ".replace(/'/g, ''')" + ".replace(/\\//g,'/')" end
Generated with the Darkfish Rdoc Generator 2.