35 lines
916 B
JavaScript
35 lines
916 B
JavaScript
|
'use strict';
|
||
|
|
||
|
class VTpeLCore {
|
||
|
constructor(options = {}) {
|
||
|
this.templates = {};
|
||
|
this.dir = options.path || '/tpl/';
|
||
|
this.suffix = options.suffix || 'tpl';
|
||
|
this.path = options.template || `${this.dir}%s.${this.suffix}`;
|
||
|
this.cache = options.cache === undefined ? true : options.cache;
|
||
|
}
|
||
|
|
||
|
async loadTemplate(name) {
|
||
|
let rawData = await fetch(this.path.replace('%s', name));
|
||
|
if (rawData.ok) {
|
||
|
let data = await rawData.text();
|
||
|
this.addTpl(name, data);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async loadArray(names) {
|
||
|
for (let name of names) {
|
||
|
await this.loadTemplate(name);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
addTpl(name, content) {
|
||
|
let temp = this.templates[name] = new VTpeLTemplate(name, content)
|
||
|
temp.parseContent(this.cache);
|
||
|
}
|
||
|
|
||
|
renderOn(name, data) {
|
||
|
return this.templates[name].render(data);
|
||
|
}
|
||
|
}
|