A very small, simple and fast EJS compiler.
> var Ejs = require('nano-ejs');
> console.log(Ejs.compile('test <?=one?>', 'one')('ola'));
test ola
> _
<!DOCTYPE>
<html><head><title><?=PAGE_TITLE?></title></head><body></body></html>
<!DOCTYPE>
<html><head><title><?=PAGE_TITLE?></title></head><body><?
if (V1) {
?>V1<?
} else {
?>V2<?
}
?></body></html>
The same as <?=global.function()?>
, where 'global' can be redefined by EJS object option 'global_id'.
This construction useful then you pass a some object to EJS function like...
var Ejs = require('nano-ejs'),
nc = require('nano-color');
var css_helpers = {
hsv2hrgb: function (h,s,v) {
return nc.rgb2hex(nc.hsv2rgb(h,s,v));
}
};
var css_text = '\
.error { color: <?.hsv2hrgb(0, 1, 1)?>; }\n\
.warning { color: <?.hsv2hrgb(120, 1, 1)?>; }\n\
';
var ejs_fn = Ejs.compile(css_text, 'css_helpers', { global_id: 'css_helpers' });
console.log(ejs_fn(css_helpers));
console output:
.error { color: #FF0000; }
.warning { color: #00FF00; }
- text
String
EJS text - args
String
compiled function arguments list (fornew Function (args, body)
) - options
Object
- open_str
String
open embedded JS code symbols sequence (the default value is '<?') - close_str
String
clode embedded JS code symbols sequence (the default value is '?>') - global_id
String
identifier of global object (likeglobal
orwindow
) (the default value is 'global')
- open_str
- options
Object
- open_str
String
open embedded JS code symbols sequence (the default value is '<?') - close_str
String
clode embedded JS code symbols sequence (the default value is '?>') - global_id
String
identifier of global object (likeglobal
orwindow
) (the default value is 'global')
- open_str
EJS parser context.
return Boolean
Check the text for EJS injections. Returns true
for EJS texts.
- text
String
EJS text
Parse EJS text and append to the final JS code.
- text
String
JS text
Parse JS with texts injections (?>test<?) and append to the final JS code.
- args
String
compiled function arguments list (for ```new Function (args, body)``)
Returns compiled JS function of EJS source.
Returns JavaScript code translated from EJS.
> console.log(new (require('nano-ejs'))().push_ejs('text<?=5?>--').push_code().listing());
"use strict";
var $ = "";
$+='text'+(5)+'--';
> _