Module: EJS
- Defined in:
- lib/ejs.rb
Constant Summary collapse
- DEFAULTS =
{ open_tag: '<%', close_tag: '%>', open_tag_modifiers: { escape: '=', unescape: '-', comment: '#', literal: '%' }, close_tag_modifiers: { trim: '-', literal: '%' }, escape: nil }
- ASSET_DIR =
File.join(__dir__, 'ruby', 'ejs', 'assets')
Class Method Summary collapse
- .compile(source, options = {}) ⇒ Object
-
.evaluate(template, locals = {}, options = {}) ⇒ Object
Evaluates an EJS template with the given local variables and compiler options.
-
.transform(source, options = {}) ⇒ Object
Compiles an EJS template to a JavaScript function.
Class Method Details
.compile(source, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/ejs.rb', line 52 def compile(source, = {}) = default() output = "function(locals, escape) {\n" output << function_source(source, ) output << "}" output end |
.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"
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ejs.rb', line 70 def evaluate(template, locals = {}, = {}) require "execjs" context = ExecJS.compile(" \#{escape_function}\n \n var template = \#{compile(template, options)}\n var evaluate = function(locals) {\n return template(locals, escape);\n }\n JS\n context.call(\"evaluate\", locals)\nend\n") |
.transform(source, options = {}) ⇒ Object
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){...}"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ejs.rb', line 36 def transform(source, = {}) = default() output = if [:escape] "import {" + [:escape].split('.').reverse.join(" as escape} from '") + "';\n" else "import {escape} from 'ejs';\n" end output << "export default function (locals) {\n" output << function_source(source, ) output << "}" output end |