Added more helpers + Improved app.template to support custom assignments

This commit is contained in:
Niko 2022-02-14 02:30:22 +01:00
parent 7ff5c076fa
commit 9e06563344
2 changed files with 9 additions and 5 deletions

View File

@ -22,7 +22,7 @@ app.template = {
return cback(html); return cback(html);
}); });
}, },
fill: function(data, tpl) { fill: function(data, tpl, customCback) {
const res = Array.from(tpl.matchAll(/\{\.[^\{\}]+\}/g)); const res = Array.from(tpl.matchAll(/\{\.[^\{\}]+\}/g));
var matches = []; var matches = [];
for (var i = 0; i < res.length; i++) { for (var i = 0; i < res.length; i++) {
@ -33,7 +33,12 @@ app.template = {
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
const k = matches[i]; const k = matches[i];
if (k.match(/^[a-zA-Z0-9_\.]+$/)) { if (k.match(/^[a-zA-Z0-9_\.]+$/)) {
const v = eval(`data.${k}`); var v = eval(`data.${k}`);
if (customCback !== undefined) {
var newv = customCback(k, v);
if (newv !== undefined)
v = newv;
}
if (v !== undefined) if (v !== undefined)
tpl = tpl.replaceAll('{.'+k+'}',v); tpl = tpl.replaceAll('{.'+k+'}',v);
} }

View File

@ -37,9 +37,8 @@ function loadScript(id, file, cback) {
document.body.appendChild(script); document.body.appendChild(script);
} }
function getNormalizedURI() { function getNormalizedURI() { return window.location.pathname.replace(/\/+?$/, '') }
return window.location.pathname.replace(/\/+?$/, ''); function capitalize(s) { return s.charAt(0).toUpperCase() + s.substr(1) }
}
function JSON_to_URLEncoded(element,key,list) { function JSON_to_URLEncoded(element,key,list) {
var list = list || []; var list = list || [];