-->

遍历帖在订单日期,使档案DocPad(Loop through posts' date in

2019-10-18 11:27发布

这里是什么,我想实现一些伪代码:

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功能。 与之配合没有问题,别的地方。 :)

Answer 1:

如果您已经按日期排序您的文章,那么你的收集已经由年度区分,一个月。 所有你需要做的是在整个采集循环,并插入您的年份和月份头时年/月值的变化。 事情是这样的:

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

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



文章来源: Loop through posts' date in order to make archive in DocPad