Template Compile, While, Else If
Pre-releaseWe have exposed the tApp.compileTemplate(html, options)
function so that you can compile templates (instead of the compile + render functions).
Additionally, we modified elseif
in templates to be either elseif
or else if
, so there is more flexibility.
Finally, we added while loops to templates. By using {% while condition %}
or {% while (condition) %}
(spaces optional), you can perform a while loop based on a certain condition. Here is an example of using a while loop to render elements of a list:
tApp.renderTemplate("./views/template.html", {
list: ["First element", "Second element", "Third element"],
i: 0
});
In the above code, we initialize a list. Additionally, we define a variable i
to 0. The reason we need to define this here is because variables in the template options are scoped to the entire template's execution (if we just declared it within a code evaluation block {{{ }}}
, the scope would be restricted to only that code evaluation block unless we use window.i
and declare the scope to be global).
<ul>
{{{@ i = 0; }}}
{% while (i < list.length) %}
<li>{{{ list[i] }}}</li>
{{{@ i++; }}}
{% endwhile %}
</ul>
In our template, we have set i = 0
in case i
was changed elsewhere, and we use the silent code execution block {{{@ }}}
to ensure that 0
is not rendered. Then, we declare our while and condition, display our list element with the code execution block, and increment i
in another silent code execution block to ensure that the value of i
is not rendered, and finally, we declare the end of the while
block.
For loops will be coming soon to make iterations easier.