Skip to content

Commit

Permalink
Template compile, while, else if
Browse files Browse the repository at this point in the history
  • Loading branch information
CMEONE committed Mar 13, 2021
1 parent 44937ca commit 32d7947
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
3 changes: 2 additions & 1 deletion example/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ tApp.route("#/template", function(request) {
returnStr += `<li>${elements[i]}</li>\n`;
}
return returnStr;
}
},
i: 0
});
});

Expand Down
24 changes: 16 additions & 8 deletions example/views/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ <h3>{{list.header}} - Loop Function From Config</h3>
<ul>
{{{ loop(list.elements); }}}
</ul>
<h3>{{list.header}} - While Loop (using {\% while %})</h3>
<ul>
{{{@ i = 0; }}}
{% while (i < list.elements.length) %}
<li>{{{ list.elements[i] }}}</li>
{{{@ i++; }}}
{% endwhile %}
</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 All @@ -34,8 +42,8 @@ <h3>{{list.header}} - Loop Function From Config</h3>
<p>if: 0 (real {{statement}})</p>
{% elseif(statement == 1) %}
<p>elseif: 1 (real {{statement}})</p>
{% elseif (statement == 2) %}
<p>elseif: 2 (real {{statement}})</p>
{% else if (statement == 2) %}
<p>else if: 2 (real {{statement}})</p>
{% else %}
<p>else: {{ statement }}</p>
{% endif %}
Expand All @@ -45,8 +53,8 @@ <h3>{{list.header}} - Loop Function From Config</h3>
<p>if: 0 (real {{statement1}})</p>
{% elseif(statement1 == 1) %}
<p>elseif: 1 (real {{statement1}})</p>
{% elseif (statement1 == 2) %}
<p>elseif: 2 (real {{statement1}})</p>
{% else if (statement1 == 2) %}
<p>else if: 2 (real {{statement1}})</p>
{% else %}
<p>else: {{ statement1 }}</p>
{% endif %}
Expand All @@ -56,8 +64,8 @@ <h3>{{list.header}} - Loop Function From Config</h3>
<p>if: 0 (real {{statement2}})</p>
{% elseif(statement2 == 1) %}
<p>elseif: 1 (real {{statement2}})</p>
{% elseif (statement2 == 2) %}
<p>elseif: 2 (real {{statement2}})</p>
{% else if (statement2 == 2) %}
<p>else if: 2 (real {{statement2}})</p>
{% else %}
<p>else: {{ statement2 }}</p>
{% endif %}
Expand All @@ -67,8 +75,8 @@ <h3>{{list.header}} - Loop Function From Config</h3>
<p>if: 0 (real {{statement3}})</p>
{% elseif(statement3 == 1) %}
<p>elseif: 1 (real {{statement3}})</p>
{% elseif (statement3 == 2) %}
<p>elseif: 2 (real {{statement3}})</p>
{% else if (statement3 == 2) %}
<p>else if: 2 (real {{statement3}})</p>
{% else %}
<p>else: {{ statement3 }}</p>
{% endif %}
Expand Down
36 changes: 32 additions & 4 deletions tApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class tApp {
static database;
static currentHash = "/";
static get version() {
return "v0.8.12";
return "v0.9.1";
}
static configure(params) {
if(params == null) {
Expand Down Expand Up @@ -397,7 +397,7 @@ class tApp {
// 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) {
static compileTemplate(html, options) {
function convertTemplate(template, parameters, prefix) {
let keys = Object.keys(parameters);
for(let i = 0; i < keys.length; i++) {
Expand Down Expand Up @@ -451,6 +451,14 @@ class tApp {
if(tokenStack[tokenStack.length - 1] == "IF" && trimmed.replaceAll(" ", "").replaceAll("\t", "") == "{%endif%}") {
tokenStack.pop();
stateStack.pop();
} else if(tokenStack[tokenStack.length - 1] == "WHILE" && trimmed.replaceAll(" ", "").replaceAll("\t", "") == "{%endwhile%}") {
stateStack[stateStack.length - 1].result = tApp.eval(tApp.optionsToEval(options) + stateStack[stateStack.length - 1].condition);
if(!stateStack[stateStack.length - 1].result) {
tokenStack.pop();
stateStack.pop();
} else {
i = stateStack[stateStack.length - 1].startLine;
}
} else if(trimmed.substring(0, 2) == "{%" && trimmed.substring(trimmed.length - 2, trimmed.length) == "%}") {
let parsedStatement = trim(trimmed.substring(2, trimmed.length - 2));
if(["if ", "if\t", "if("].includes(parsedStatement.substring(0, 3))) {
Expand All @@ -472,15 +480,35 @@ class tApp {
} else {
throw "tAppError: Else-if missing an if-statement on line " + (i + 1) + ".";
}
} else if(["else if ", "else if\t", "else if("].includes(parsedStatement.substring(0, 8))) {
if(tokenStack[tokenStack.length - 1] == "IF") {
if(!stateStack[stateStack.length - 1].executed) {
let condition = trim(parsedStatement.substring(7));
stateStack[stateStack.length - 1].result = tApp.eval(tApp.optionsToEval(options) + condition);
stateStack[stateStack.length - 1].executed = stateStack[stateStack.length - 1].result;
} else {
stateStack[stateStack.length - 1].result = false;
}
} else {
throw "tAppError: Else-if missing an if-statement on line " + (i + 1) + ".";
}
} else if(trimmed.replaceAll(" ", "").replaceAll("\t", "") == "{%else%}") {
if(tokenStack[tokenStack.length - 1] == "IF") {
stateStack[stateStack.length - 1].result = !stateStack[stateStack.length - 1].executed;
stateStack[stateStack.length - 1].executed = stateStack[stateStack.length - 1].result;
} else {
throw "tAppError: Else missing an if-statement on line " + (i + 1) + ".";
}
} else if(["while ", "while\t", "while("].includes(parsedStatement.substring(0, 6))) {
tokenStack.push("WHILE");
let condition = trim(parsedStatement.substring(5));
stateStack.push({
condition: condition,
result: tApp.eval(tApp.optionsToEval(options) + condition),
startLine: i
});
}
} else if((tokenStack[tokenStack.length - 1] == "IF" && stateStack[stateStack.length - 1].result) || tokenStack[tokenStack.length - 1] == null) {
} else if((tokenStack[tokenStack.length - 1] == "IF" && stateStack[stateStack.length - 1].result) || tokenStack[tokenStack.length - 1] == null || (tokenStack[tokenStack.length - 1] == "WHILE" && stateStack[stateStack.length - 1].result)) {
let newRes = splitLines[i];
let it = newRes.matchAll(new RegExp("{{{@[\\s|\\t]*(.+?(?=}}}))[\\s|\\t]*}}}", "g"));
let next = it.next();
Expand Down Expand Up @@ -509,7 +537,7 @@ class tApp {
return newHTML;
}
static renderTemplateHTML(html, options) {
html = tApp.templateToHTML(html, options);
html = tApp.compileTemplate(html, options);
tApp.render(html);
}
static renderTemplate(path, options) {
Expand Down

0 comments on commit 32d7947

Please sign in to comment.