Skip to content

Commit

Permalink
Template functions
Browse files Browse the repository at this point in the history
  • Loading branch information
CMEONE committed Mar 13, 2021
1 parent 6c14e79 commit dcfdb31
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
9 changes: 8 additions & 1 deletion example/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,14 @@ tApp.route("#/template", function(request) {
statement5: 6,
statement6: 8,
statement7: 7,
testVal: 10
testVal: 10,
loop: function(elements) {
let returnStr = "";
for(let i = 0; i < elements.length; i++) {
returnStr += `<li>${elements[i]}</li>\n`;
}
return returnStr;
}
});
});

Expand Down
7 changes: 6 additions & 1 deletion example/views/template.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{# Comments #}
<h1>{{ header }}</h1>
{% if(text != null) %}
<p>{{text}}</p>
Expand All @@ -12,12 +13,16 @@ <h1>{{ header }}</h1>
<p>More: {{ text }}</p>
{% endif %}
<p>Template text above uses {\{text}}</p>
<h3>{{list.header}}</h3>
<h3>{{list.header}} - Manual</h3>
<ul>
<li>{{list.elements.0}}</li>
<li>{{list.elements.1}}</li>
<li>{{list.elements.2}}</li>
</ul>
<h3>{{list.header}} - Loop Function From Config</h3>
<ul>
{{{ loop(list.elements); }}}
</ul>
<p>1 + 1 = {{{ 1 + 1 }}} ({{\{ 1 + 1 }}})</p>
<p>10 + 5 = {{{ testVal += 3; testVal + 2; }}} ({{\{ testVal += 3; testVal + 2; }}})</p>
<p>10 + 3 = {{{ testVal }}} ({{\{ testVal }}})</p>
Expand Down
38 changes: 24 additions & 14 deletions tApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,14 @@ class tApp {
let evalStr = "";
let keys = Object.keys(data);
for(let i = 0; i < keys.length; i++) {
try {
evalStr += "let " + keys[i] + " = " + JSON.stringify(data[keys[i]]) + ";";
} catch(err) {
evalStr += "let " + keys[i] + " = " + data[keys[i]] + ";";
if(typeof data[keys[i]] == "function") {
evalStr += "let " + keys[i] + " = " + data[keys[i]].toString() + ";";
} else {
try {
evalStr += "let " + keys[i] + " = " + JSON.stringify(data[keys[i]]) + ";";
} catch(err) {
evalStr += "let " + keys[i] + " = " + data[keys[i]] + ";";
}
}
}
return evalStr;
Expand All @@ -390,7 +394,8 @@ class tApp {
if(data == null) {
data = {};
}
return tApp.eval(tApp.optionsToEval(data) + "let _____result = (function() {return eval(\"" + code.replaceAll("\"", "\\\"") + "\")})();" + tApp.restoreOptions(data) + "[_____result, _____returnOptions]");
// return tApp.eval(tApp.optionsToEval(data) + "let _____result = (function() {return eval(\"" + code.replaceAll("\"", "\\\"") + "\")})();" + tApp.restoreOptions(data) + "[_____result, _____returnOptions]");
return tApp.eval(tApp.optionsToEval(data) + "let _____result = " + code + ";" + tApp.restoreOptions(data) + "[_____result, _____returnOptions]");
}
static templateToHTML(html, options) {
function convertTemplate(template, parameters, prefix) {
Expand Down Expand Up @@ -426,6 +431,12 @@ class tApp {
returnStr = returnStr.substring(0, index + 1);
return returnStr;
}
let it = html.matchAll(new RegExp("{#.+?(?=#})#}", "g"));
let next = it.next();
while(!next.done) {
html = html.replace(next.value[0], "");
next = it.next();
}
html = html.replaceAll("{{\\{", "{{\\\\{");
html = html.replaceAll("{{{", "{{\\{");
html = convertTemplate(html, options, "");
Expand Down Expand Up @@ -469,24 +480,23 @@ class tApp {
throw "tAppError: Else missing an if-statement on line " + (i + 1) + ".";
}
}
} else if(tokenStack[tokenStack.length - 1] == "IF" && stateStack[stateStack.length - 1].result) {
} else if((tokenStack[tokenStack.length - 1] == "IF" && stateStack[stateStack.length - 1].result) || tokenStack[tokenStack.length - 1] == null) {
let newRes = splitLines[i];
let it = newRes.matchAll(new RegExp("{{{[\\s|\\t]*(.+?(?=}}}))[\\s|\\t]*}}}", "g"));
let it = newRes.matchAll(new RegExp("{{{@[\\s|\\t]*(.+?(?=}}}))[\\s|\\t]*}}}", "g"));
let next = it.next();
while(!next.done) {
console.log(next.value[1]);
let contextEval = tApp.evalInContext(trim(next.value[1]), options);
options = contextEval[1];
newRes = newRes.replace(next.value[0], contextEval[0]);
newRes = newRes.replace(next.value[0], "");
next = it.next();
}
newHTML += newRes + "\n";
} else if(tokenStack[tokenStack.length - 1] == null) {
let newRes = splitLines[i];
let it = newRes.matchAll(new RegExp("{{{[\\s|\\t]*(.+?(?=}}}))[\\s|\\t]*}}}", "g"));
let next = it.next();
it = newRes.matchAll(new RegExp("{{{[\\s|\\t]*(.+?(?=}}}))[\\s|\\t]*}}}", "g"));
next = it.next();
while(!next.done) {
let contextEval = tApp.evalInContext(trim(next.value[1]), options);
if(contextEval[0] == null) {
contextEval[0] = "";
}
options = contextEval[1];
newRes = newRes.replace(next.value[0], contextEval[0]);
next = it.next();
Expand Down

0 comments on commit dcfdb31

Please sign in to comment.