1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
module.exports = format;
function format(text) {
var context;
if (typeof arguments[1] == 'object' && arguments[1]) {
context = arguments[1];
} else {
context = Array.prototype.slice.call(arguments, 1);
}
return String(text).replace(/\{?\{([^{}]+)}}?/g, replace(context));
};
function replace (context, nil){
return function (tag, name) {
if (tag.substring(0, 2) == '{{' && tag.substring(tag.length - 2) == '}}') {
return '{' + name + '}';
}
if (!context.hasOwnProperty(name)) {
return tag;
}
if (typeof context[name] == 'function') {
return context[name]();
}
return context[name];
}
}
|