For loops can be used in HubL to iterate through sequences of objects.
{% for %}
statement and end with an {% endfor %}
statement. Within the {% for %}
statement a single sequence item is named followed by in
and then the name of the sequence. The code between the opening and closing for
statements is printed with each iteration, and generally includes the printed variable of the individual sequence item. Below is the basic syntax of a for loop:
loop.index
keeps a count of the current number of the iterations of the loop. There are several other loop variable properties that count the iterations in different ways. These properties are described below:
Variable | Description |
---|---|
loop.cycle | A helper function to cycle between a list of sequences. See the explanation below. |
loop.depth | Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 1 |
loop.depth0 | Indicates how deep in deep in a recursive loop the rendering currently is. Starts at level 0 |
loop.first | This variable evaluates to true, if it is the first iteration of the loop. |
loop.index | The current iteration of the loop. This variable starts counting at 1. |
loop.index0 | The current iteration of the loop. This variable starts counting at 0. |
loop.last | This variable evaluates to true, if it is the last iteration of the loop. |
loop.length | The number of items in the sequence. |
loop.revindex | The number of iterations from the end of the loop. Counting down to 1. |
loop.revindex0 | The number of iterations from the end of the loop. Counting down to 0. |
loop.index
to keep a count that is printed with each iteration.
divisibleby
certain numbers. It then renders the width of the post-item div accordingly. The example uses the standard blog post loop and assumes that there are 6 posts in the loop.
<ul>
within a <ul>
of parent items.
odd
and even
are applied to posts in a listing (the example assumes that there are 5 posts in the loop).
Please note that there are no spaces between the comma-separated cycle string values.
id
to the wrapping HTML. That tag is unique per tag “name”. In situations where you need to use a tag in a for loop setting unique names is not practical. Add the unique_in_loop
parameter to your tag to generate unique ids. This parameter appends the module name with the current loop iteration number, ensuring that it is unique. Unique id’s are not only needed for valid HTML but are important for accessibility.