Control Flow constructs
Jtwig implements basic control flow constructs such as if
conditions and for
loops with the exact same syntax as Twig.
if
conditions
If conditions are the simpliest control flow in Jtwig. It supports consecutive elseif
conditions and the common else
construct too.
{% if (expression) %}
... content if expression is evaluated to true ...
{% elseif (anotherExpression) %}
... content if anotherExpression is evaluated to true ...
{% else %}
... content if none of the previous conditions are met ...
{% endif %}
Note that if constructs will only output the content of the first block which condition is evaluated to true. The way Jtwig evaluates an expression to true is configurable and will be explained further on this manual.
In terms of variable scoping, if inner content shares the context with the parent context, this means if conditions can affect variables in the outer scope.
{% set variable = "a" %}
{% if (true) %}
{% set variable = "b" %}
{% endif %}
{{ variable }}
The previous example will output b
which illustrates how scoping works on if conditions by sharing the context with the parent construct.
for
loops
Again based on Twig, Jtwig also implements, in a similar fashion for
loops. It allows one to iterate over a list or a map. In this specific a list must be seens as a map where the key is the index.
{# Example with list #}
{% for item in list %}
... content using variable item ...
{% endfor %}
{# Example with map #}
{% for key, value in map %}
... content using variables key and value ...
{% endfor %}
Similar to the boolean expression evaluation, list or map evaluation in Jtwig is also configurable and will be discussed further on in this manual.
loop
variable
For loops come with an extra variable defined in the context, the loop
variable. The loop variable provides a set of useful properties when within a for loop, check below the properties exposed by this variable.
Property | Description |
---|---|
length |
Size of the list or map being iterated |
index |
Current iteration count starting in 1 |
index0 |
Current iteration count starting in 0 |
revindex |
Remaining number of iterations to reach the end of the list or map, ends in 1 |
revindex0 |
Remaining number of iterations to reach the end of the list or map, ends in 0 |
first |
Boolean property only true in the first iteration |
last |
Boolean property only true in the last iteration |
parent |
Accessing the parent context |
{% for item in [1, 2, 3] %}
{% if (loop.first) %}
Start
{% endif %}
{% endfor %}
The previous example will output Start
only once.
This loop
variable is only bound to the for context, it means this variable will not be visible outside of the for loop scope.
If there is a loop
variable in the context, it will not be overriden but to access it one need to get the parent context, for example:
{% set loop = 1 %}
{% for item in [1, 2, 3] %}
{% if (loop.first) %}
{{ loop.parent.loop }}
{% set loop = 2 %}
{% endif %}
{% endfor %}
{{ loop }}
The previous template will print 1 2
. Note that setting loop
inside the for loop will write to the context, but won't override the for loop bound variable.