Instantly share code, notes, and snippets.

What would you like to do?
DocPad: Query custom category objects (via parse event)
# Prepare
_ = require('underscore')
categories = {}
# Configure DocPad
docpadConfig =
templateData: ->
getDocumentsForCategory: (categoryId) ->
return @getCollection('documents').findAll(categories: $has: categoryId)
getCategoriesForDocument: (document) ->
document ?= @getDocument()
categoryIds = document.get('categories')
categories = @categories[categoryId] for categoryId in categoryIds
return categories
getCategories: ->
return categories
parseAfter: ->
@getCollection('documents').forEach (document) ->
documentCategories = document.getMeta().get('categories')
for documentCategory in documentCategories
# Update the global categories listing with our document's categories
categories[] ?= {}
_.extend(categories[], documentCategory)
# Update our document's categories with a simple id listing
document.set(categories: _.pluck(documentCategories, 'id'))
# Export
module.exports = docpadConfig
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