Iteration
Pug’s first-class iteration syntax makes it easier to iterate over arrays and objects in a template:
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
ul
each val, index in ['zero', 'one', 'two']
li= index + ': ' + val
<ul>
<li>1: one</li>
</ul>
Pug also lets you iterate over the keys in an object:
<ul>
<li>1: one</li>
<li>2: two</li>
<li>3: three</li>
</ul>
- var values = [];
ul
each val in values.length ? values : ['There are no values']
li= val
<ul>
</ul>
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:
<li>There are no values</li>
</ul>
while
You can also use while
to create a loop:
- var n = 0;
ul
while n < 4
li= n++
<ul>
<li>0</li>
<li>1</li>
<li>2</li>
<li>3</li>