Outputs
The Stencil language uses the single character '$' to demarcate dynamic outputs and closes them with the ';' character. All outputs take the form
$<command>;Each dynamic output command is detailed below.
expression
Output the value of an expression
$user.name; $2+2;
John Doe 5
if/else
Conditionally output a block of text
$if value {
value is true
}
value is true
An else block can be provided in case the value is false
$if value {
value is true
}
else {
value is false
};
value is false
foreach/else
Loop through a collection and output a block for each value
$foreach item in items {
Color: $item.color;
}
Color: Orange Color: Blue Color: Black
An else block can be provided to output in case the collection is empty
$foreach item in items {
$item.name;
}
else {
No Items
};
No Items
An iteration variable can be specified to get common useful information on the iteration being processed. The iteration variable has these properties:
- index - iteration # starting from 0
- count - iteration # starting from 1
- even - true if it is an even numbered iteration
- odd - true if it is an odd numbered iteration
- hasNext - true if this is not the last iteration
$foreach item,it in items {
$it.count: $item.color;
}
1: Orange 2: Blue 3: Black
while
Loop while an expression is true
$white value < 5 {
Index: $value;
$assign value=value+1;
}
Index: 0 Index: 1 Index: 2 Index: 3 Index: 4
switch/case/default
Select one of several blocks based on a value
$switch value
case 0 {
Number: 0
}
case 1 {
Number: 1
}
case 'test' {
Text: test
};
Text: test
A default block can be provided in case the value matches none of the cases
$switch value
case 0 {
Number: 0
}
case 1 {
Number: 1
}
default {
No Match
};
No Match
with
Add properties of one or more expressions to the current scope
$with stock,group {
Group: $title << for group.title
Name: $name; << for stock.name
Symbol: $symbol;<< for stock.symbol
Price: $price; << for stock.price
};
Group: Technology Name: Apple Inc. Symbol: AAPL Price: 489.56
Any expression can be provided
$with [1,2,3] {
Items: $size << for [].size
};
Items: 3
declare variable
Declare variables in the current scope.
$var x=5, y=10; $x; $y;
5 10
assign variable
Assign values to variables in the current scope. The variable must already be declared.
$assign x=10, y=20; $x; $y;
10 20
break
Breaks out of the nearest loop
$while x < 10 {
$if x == 3 {
$break;
};
Value: $x;
};
Value: 0 Value: 1 Value: 2 Value: 3
continue
Continues on to the next iteration of the nearest loop
$while x < 3 {
Value: $x;
$continue;
This wont get printed
};
Value: 0 Value: 1 Value: 2
include
Include another template using a file path relative to the currently processing template.
$include 'layout';
Included Text
Parameters can be passed to the included template
$include 'layout' (title='Top 100');
Title: Top 100
Blocks can be passed to the included template as well
$include 'layout' (title='Top 100')
header {
## HEADER ##
}
content {
## CONTENT ##
}
footer {
## FOOTER ##
};
Title: Top 100 ## HEADER ## ## CONTENT ## ## FOOTER ##