Skip to content

Instantly share code, notes, and snippets.

@balupton
Last active December 11, 2015 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balupton/4556245 to your computer and use it in GitHub Desktop.
Save balupton/4556245 to your computer and use it in GitHub Desktop.
DocPad: Query custom category objects (via template helper only)
# Configure DocPad
docpadConfig =
templateData:
# 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.id, category.slug]
return true
return false
.query()
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