Add template.js to renderer for basic templating

This commit is contained in:
chylex 2016-10-25 22:46:55 +02:00
parent 2a5f430884
commit 7f654eaf5d

View File

@ -0,0 +1,18 @@
var TEMPLATE_REGEX = /{([^{}]+?)}/g;
var TEMPLATE = function(contents){
this.contents = contents;
};
TEMPLATE.prototype.apply = function(obj, processor){
return this.contents.replace(TEMPLATE_REGEX, (full, match) => {
var value = match.split(".").reduce((o, property) => o[property], obj);
if (processor){
var updated = processor(match, value);
return typeof updated === "undefined" ? value : updated;
}
return value;
});
};