If Else Statement
Evaluate a conditional expression, if the outcome of the expression is non nil, false, or 0, execute the block immediately following the if statement. Otherwise, execute the block following the optional else statement.
Usage Restrictions
1.9.1 or later.
Samples
| | Output |
| To conditionally include a template: {{
if(date.hours(date.now) <= 12) {
template.stub()
}
}} | | |
| To conditionally emit one text or another: {{
if(user.anonymous) {
"Welcome guest. Please register!"
} else {
"Welcome back ".. user.name .. "!"
}
}} | | Welcome guest. Please register! |
Foreach Statement
The foreach statements enables iteration over list, map, and xml values. The foreach statement also sets the the global variable __count to indicate how many iterations it has performed so far.
Usage Restrictions
8.05.1 or later.
Samples
| | Output |
| Iterating over a list: {{ foreach(var x in [ 1, 2, 3 ]) { x } }} | | 123 |
| Iterating over a map: {{ foreach(var x in { a: 1, b: 2, c: 3 }) { x } }} | | 123 |
| Iterating over an xml document: {{
var doc = web.xml(page.api);
foreach(var x in doc['//page.parent']) {
xml.text(x, '@id'); ' '
}
}} | | 11333 10668 2328 1 |
Var Statement
The var statement declares a new variable in the current scope. Variables with the same name outside the current scope are not affected. The declared variable remains accesible until the current scope ends.
Usage Restrictions
8.05.1 or later.
Samples
| | Output |
| Declare a variable twice, inside and outside of a nested scope: {{
var x = 10;
if(true) {
var x = 11;
}
x;
}} | | 10 |
Let Statement
The let statement assigns a new value to an existing variable. If the variable does not exist, it is declared as if the let statement were a var statement.
Usage Restrictions
8.05.1 or later.
Samples
| | Output |
| Reassign a variable twice of a nested scope: {{
var x = 10;
if(true) {
let x = 11;
}
x;
}} | | 11 |
| Increment a variable: {{ var x = 10; let x += 2; x; }} | | 12 |
| Decrement a variable: {{ var x = 10; let x -= 2; x; }} | | 8 |
| Mutiply a variable: {{ var x = 10; let x *= 2; x; }} | | 20 |
| Divide a variable: {{ var x = 10; let x /= 2; x; }} | | 5 |
| Compute the modulo of a variable: {{ var x = 10; let x %= 2; x; }} | | 0 |
| Concatenate to a variable: {{ var x = "hello "; let x ..= "world"; x; }} | | hello world |