如何在Jade中反向循环for循环?

时间:2023-01-23 21:09:52
ul.blogpostGrid
for child in page.get('/posts').children
    a(href=child.url): li.col.span_1_of_4 
        h2 #{child.title}
        div.imgThumb(style='background-image: url(#{child.url}thumb.jpg);')
        p #{child.excerpt}

The For Loop currently displays the blogposts in ascending order of creation. How can I reverse the order of displaying them? Is there a reverse function? If yes, how can it be implemented in this context? I'm using woods cms. https://github.com/studiomoniker/woods

For循环当前以创建的升序显示博客帖子。如何反转显示它们的顺序?有反向功能吗?如果是,如何在这种情况下实施?我正在使用森林cms。 https://github.com/studiomoniker/woods

2 个解决方案

#1


2  

This helped.

for child in page.get('/posts').children.reverse()

#2


0  

You could use while to iterate over the posts backwards:

您可以使用while来向后迭代帖子:

ul.blogpostGrid
- var posts = page.get('/posts').children;
- var postIndex = posts.length;
while postIndex >= 0
    a(href=posts[postIndex--]): li.col.span_1_of_4 
        h2 #{posts[postIndex--].title}
        div.imgThumb(style='background-image: url(#{posts[postIndex--].url}thumb.jpg);')
        p #{posts[postIndex--].excerpt}

#1


2  

This helped.

for child in page.get('/posts').children.reverse()

#2


0  

You could use while to iterate over the posts backwards:

您可以使用while来向后迭代帖子:

ul.blogpostGrid
- var posts = page.get('/posts').children;
- var postIndex = posts.length;
while postIndex >= 0
    a(href=posts[postIndex--]): li.col.span_1_of_4 
        h2 #{posts[postIndex--].title}
        div.imgThumb(style='background-image: url(#{posts[postIndex--].url}thumb.jpg);')
        p #{posts[postIndex--].excerpt}