迭代(Iteration)
¥Iteration
Pug 支持两种主要的迭代方法:each
和 while
。
¥Pug supports two primary methods of iteration: each
and while
.
each(each)
Pug 的一流迭代语法使迭代模板中的数组和对象变得更加容易:
¥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 还允许你迭代对象中的键:
¥Pug also lets you iterate over the keys in an object:
要迭代的对象或数组只是普通的 JavaScript。因此,它可以是变量,也可以是函数调用的结果,或者几乎任何其他东西。
¥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.
还可以添加一个 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:
你还可以使用 for
作为 each
的别名。
¥You can also use for
as an alias of each
.
while(while)
你还可以使用 while
创建循环:
¥You can also use while
to create a loop: