1

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

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

现在它什么也不输出。所以我想我只是把一些变量名弄错了,我想我做到了。我很感激任何帮助。:)

顺便说一句,不要担心@postDatetime功能。这在其他地方没有问题。:)

4

1 回答 1

2

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

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()

这听起来像你所追求的吗?

于 2013-09-12T14:39:14.837 回答