循环

循环(Loops)

你可以使用 @each 标签遍历 ObjectsArrayseach 标签的内部工作方式类似于 JavaScript 的 for of 循环。

@each(user in users)
<li> {{ user.username }} </li>
@end
@each((user, index) in users)
<li> {{ index + 1 }} {{ user.username }} </li>
@end

遍历对象

你可以使用同一个 @each 标签遍历 JavaScript 对象。例如:

await edge.render('recipes', {
food: {
ketchup: '5 tbsp',
mustard: '1 tbsp',
pickle: '0 tbsp'
}
})
@each((amount, ingredient) in food)
<li> Use {{ amount }} of {{ ingredient }} </li>
@end

定义兜底内容

@each 标签可以与 @else 标签配合使用,用于在值为空数组/空对象或 undefined 的情况下定义兜底内容。

@each(comment in post.comments)
@include('partials/comment')
@else
<p> This post has not received any comments </p>
@end