Instantly share code, notes, and snippets.

@balupton /
Last active Dec 11, 2015

What would you like to do?
DocPad: Query custom category objects (via template helper only)
# Configure DocPad
docpadConfig =
# Get a collection of documents with a particular category
# Using QueryEngine's setFilter
getDocumentsWithCategory: (desiredCategory) ->
resultCollection = @getCollection('documents').createChildCollection()
.setFilter 'category', (model) ->
categories = model.get('categories')
return false unless Array.isArray(categories)
for category in categories
if desiredCategory in [, category.slug]
return true
return false
return resultCollection
# Export
module.exports = docpadConfig
<%- @getDocumentsWithCategory('a').pluck('title') %>
title: 'Happy new year!'
layout: 'post'
categories: [
		id: 'a'
		description: 'a is the first letter of the alphabet'
		id: 'b'
		description: 'b is the second letter of the alphabet'

Happy new year. Boom boom.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment