vegito
Version:
Simple micro templating with JavaScript expression support.
36 lines (27 loc) • 872 B
JavaScript
;
var index = function (
tpl,
data,
ref
) {
if ( tpl === void 0 ) tpl = '';
if ( data === void 0 ) data = {};
if ( ref === void 0 ) ref = {};
var delimiters = ref.delimiters; if ( delimiters === void 0 ) delimiters = ['{{', '}}'];
var safe = ref.safe; if ( safe === void 0 ) safe = false;
var sanitize = ref.sanitize;
var re = new RegExp(((delimiters[0]) + "([\\s\\S]+?)" + (delimiters[1])), 'g');
return tpl.replace(re, function (_, exp) {
if (!safe) {
exp = sanitize ? sanitize(exp) : exp;
return new Function('data', ("with (data) {return " + exp + "}"))(data) // eslint-disable-line no-new-func
}
var ret = data;
for (var i = 0, list = exp.split('.'); i < list.length; i += 1) {
var prop = list[i];
ret = ret ? ret[prop] : '';
}
return ret || ''
})
};
module.exports = index;