Search code examples
coffeescriptdocpadeco

How to show the "upcoming" content based on its date on Docpad?


I'm trying to promote a content on a page. This content should the upcoming content, i.e. the next one in all of the same type ("event").

Here's how metadata look like :

---
layout: event
date: 2013-12-11
title: Cool event
---

The docpad.coffee file collections configuration looks like (using moment.js):

collections:
    events: (database) ->
        database.findAllLive(type: "event", [date: -1])

    next_events: (database) ->
        @getCollection("events")
            .setFilter("upcoming", (event, searchString) ->
                return moment(event.eventDate, "YYYY-MM-DD").diff(moment()) < 0
            )

Finally in the index.html.eco :

<% for event in @getCollection("next_events").toJSON(): %>
    <h3><%= event.title %></h3>
<% end %>

Problem: it shows all my events, not only the future ones. When I use the events collection it works as intended (all events ordered by date desc). But no difference when I use the next_events collection. I know that to show only one I'll need to reduce the collection content with something like <% for event in @getCollection("next_events").toJSON()[0]: %>.

Any pointer greatly appreciated.


Solution

  • Try this:

    next_events: (database) ->
        @getCollection("events")
            .createLiveChildCollection()
            .setFilter("upcoming", (event, searchString) ->
                return moment(event.eventDate, "YYYY-MM-DD").diff(moment()) < 0
            )
            .query()