Skip to content Skip to sidebar Skip to footer

Loop Through Posts' Date In Order To Make Archive In 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 That's the ps

Solution 1:

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', ->forpostin @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?

Post a Comment for "Loop Through Posts' Date In Order To Make Archive In Docpad"