Query builder - limburgie/c3s GitHub Wiki

This page gives a full reference of the different options when building content queries in C3S. The examples given are in FreeMarker. The same methods are available in the other supported templating engines.

Get all items

${api.query("book").findAll()}

Returns an empty list if no books are found.

Get limited amount of items

${api.query("book").findAll(5)}

Returns a maximum of 5 books. Equals to findAll(1, 5).

Get paginated items

${api.query("book").findAll(2, 5)}

Returns the 2nd page of maximum 5 books.

Get one item

${api.query("book").findOne()}

Returns null if no results are found. Returns the first item in the list if more than one is found.

Order by text value

${api.query("book").orderByAsc("title").findAll()}
${api.query("book").orderByDesc("title").findAll()}

Returns books ordered by their title (ascending or descending).

In Prismic.io you can chain orderings:

${api.query("book").orderByDesc("year").orderByAsc("title").findAll()}

Filter by text value

${api.query("book").with("genre", "Science Fiction").findAll()}

Returns all books in the "Science Fiction" genre. Filters can be chained:

${api.query("book").with("genre", "Science Fiction").with("year", "2010").findAll()}

Filter by reference

Find all books written by a certain author:

<#assign tolkien = api.query("author").with("name", "Tolkien").findFirst()>
${api.query("book").with("author", tolkien).findAll()}

Filter by date

Dates are queried just like text values. If you want to match today's date, you can use the short-hand withDateToday method.

${api.query("event").with("date", "2018-01-01").findAll()}
${api.query("event").withDateToday("date").findAll()}

You can also query date ranges, useful for e.g. listing past or upcoming events:

${api.query("event").withDateInPast("date").findAll()}       // Returns past events up to now
${api.query("event").withDateInFuture("date").findAll()}     // Returns upcoming event from now on

The withDateInPast and withDateInFuture methods take the current timestamp into account. You can also query items with a date before the start or after the end of today's date:

${api.query("event").withDateBeforeToday("date").findAll()}   // Returns past events up to the start of this day
${api.query("event").withDateAfterToday("date").findAll()}    // Returns upcoming events from tomorrow on

Get total item count

${api.query("book").with("genre", "Science Fiction").count()}

Get random item(s)

${api.query("book").findRandom()}

Retrieves 1 random book.

${api.query("book").findRandom(5)}

Retrieves at most 5 random books.