audio-vis/raw/javascript/template.js

42 lines
1,017 B
JavaScript

/** @deprecated use VTepL instead! */
class Template {
constructor() {
this.tpl = {};
}
async loadTemplate(name) {
let self = this;
if (!this.tpl[name]) {
self.tpl[name] = await FetchHandler.loadFile(templateDir + name + '.tpl', false)
}
}
async loadArray(names) {
for (let name of names) {
await this.loadTemplate(name);
}
}
parseTemplate(name, data) {
if (!this.tpl[name]) {
return ""
}
let m, d = this.tpl[name];
while ((m = templateEx.exec(d)) !== null) {
if (m.index === templateEx.lastIndex) {
templateEx.lastIndex++;
}
let key = m[0];
let value = data[m[1]];
if (value === undefined || value === null) {
value = "";
}
d = d.replace(key, value)
}
return d;
}
}
const templateEx = /\$(.*?)\$/gm;
const templateDir = "/out/tpl/"