Iteration
Pug supports two primary methods of iteration: each
and while
.
each
Pug’s first-class iteration syntax makes it easier to iterate over arrays and objects in a template:
You can also get the index as you iterate:
Pug also lets you iterate over the keys in an object:
The object or array to iterate over is just plain JavaScript. So, it can be a variable, or the result of a function call, or almost anything else.
One can also add an else
block that will be executed if the array or object does not contain values to iterate over. The following is equivalent to the example above:
You can also use for
as an alias of each
.
while
You can also use while
to create a loop: