循环查看帖子的日期,以便在DocPad中进行归档 [英] Loop through posts' date in order to make archive in DocPad

查看:59
本文介绍了循环查看帖子的日期,以便在DocPad中进行归档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Here's some pseudo-code of what I want to achieve:

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

That's the pseudo-code. However I don't have enough experience to achieve this. The file in which this would happen is this one: https://github.com/Greduan/eduantech.docpad/blob/master/src/documents/posts.html.eco

它将是 eco 语言.如果有必要,我也会使用Moment.js.

And it would be in the eco language. I'm using Moment.js as well in case that's necessary.

即使您没有提供确切的代码,也将非常感谢您提供一般指导. :)

Even if you don't provide the exact code, a general direction will be very appreciated. :)

我想要实现的类似于以下内容: http://swannodette.github.io/archive.html

What I would like to achieve is something similar to this: http://swannodette.github.io/archive.html

这是我想到的一些代码:

EDIT 2: Here's some of the code that I came up with:

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

目前,它什么也不输出.所以我想我只是弄错了一些变量名,我想我确实做错了.感谢您的帮助. :)

For now it just outputs nothing. So I'm thinking I just got some of the variable names wrong, which I imagine I did. I appreciate any help. :)

BTW不必担心@postDatetime功能.那与其他地方没有问题. :)

BTW don't worry about the @postDatetime function. That with works no problems somewhere else. :)

推荐答案

如果您已经按日期对帖子进行了排序,则您的收藏集已经按年,月分组了.您需要做的就是遍历整个集合,并在年/月值更改时插入您的年和月标头.像这样:

If you already have your posts sorted by date, then your collection is already grouped by year,month. All you need to do is loop through the whole collection and insert your year and month headers when the year/month values change. Something like this:

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

听起来像你在追求什么吗?

Does that sound like what you are after?

这篇关于循环查看帖子的日期,以便在DocPad中进行归档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆