循环通过帖子'日期,以便在DocPad中进行存档

Loop through posts' date in order to make archive in DocPad

本文关键字:DocPad 日期 循环      更新时间:2023-09-26

下面是我想要实现的一些伪代码:

for year in post.date
    h1 year
    for month in post.date
        h2 month
        ul
            li post entry

这就是伪代码。然而,我没有足够的经验来做到这一点。发生这种情况的文件如下:https://github.com/Greduan/eduantech.docpad/blob/master/src/documents/posts.html.eco

这将是生态语言。我也在使用Moment.js,以备不时之需。

即使您没有提供确切的代码,我们也将非常感谢您的指导。:)

编辑:我想要实现的目标与此类似:http://swannodette.github.io/archive.html

第2版:以下是我想出的一些代码:

for post in @getCollection('posts').toJSON()
    for year in post.date
        h1 @moment(post.date).format('YYYY')
        for month in post.date
            h2 @moment(post.date).format('MMMM')
            ul ->
                li ->
                    @postDatetime(post.date, 'll') + ' » '
                    a href:'post.url', post.title

目前,它什么也没输出。所以我想我只是弄错了一些变量名,我想我确实弄错了。我很感激任何帮助。:)

BTW不必担心@postDatetime函数。在其他地方工作没有问题。:)

如果您已经按日期对帖子进行了排序,那么您的收藏已经按年、月进行了分组。您所需要做的就是遍历整个集合,并在年/月值更改时插入年和月标题。类似这样的东西:

yr = -1 //temporary vars for storing current year value in loop
mnth = -1 //same for month value
monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]
div style:'text-align:left;font-size:20px;width:500px;margin-right:auto;margin-left:auto', ->
for post in @getCollection('posts').toJSON()
    if post.date.getFullYear() isnt yr
        yr = post.date.getFullYear()
        mnth = -1 
        h1 yr.toString()
    if post.date.getMonth() isnt mnth
        mnth = post.date.getMonth() 
        h2 style:'padding-left:10px;', monthNames[mnth]
        ul style:'padding-left:50px;', ->
    li ->
        post.date.toDateString()

这听起来像你想要的吗?